Tuesday, April 1, 2014

How to make keyboard event handler in delphi component

Ctrl+V를 수신받을때 KeyPress 핸들러에 이상한 코드가 들어온다.
KeyPress를 받지 않을 방법은 없고 KeyPress안에서 Ctrl키가 눌려있는지를
판단해서 단축키에 대한 올바른 처리를 할 수 있다.



KeyPress handler raise after every KeyDown event that handled or not handled.
Example, User press Ctrl + V , KeyDown and KeyPress and KeyUp event called.


in class interface

    procedure KeyPress(var Key: Char); override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;


in implementation

procedure TClassXXX.KeyDown(var Key: Word; Shift: TShiftState);
begin
   inherited;

    if Key = VK_LEFT then
    begin
       .....
    end;
end;

procedure TClassXXX.KeyPress(var Key: Char);
var
    ShiftState: TShiftState;
    KeyState: TKeyboardState;
begin
    inherited;

    GetKeyboardState(KeyState);
    ShiftState := KeyboardStateToShiftState(KeyState);

    if not ( ssCtrl In ShiftState ) then
    begin
          // key press without control key
    end;
end;


No comments:

Post a Comment