Search code examples
delphitype-conversionkeypress

Delphi keypress and keybd_event


I have 2 components. In the first one I write a keyboard character, I focuse the second then I use keybd_event to send the keyboard press (key down and key up). But if I press "o" I have ord(Key) which is 111 (OK for the ascii table). But keybd_event prints a /

procedure TAutoComplete.AutoCompleteKeyPress(Sender: TObject; var Key: Char);
var
    iKeyValue: integer;
begin
    FXEdit.SetFocus;

    iKeyValue := ord(Key);

    keybd_event(iKeyValue, MapvirtualKey(iKeyValue, 0), 0, 0);
    keybd_event(iKeyValue, MapvirtualKey(iKeyValue, 0), KEYEVENTF_KEYUP, 0);
end;

How can I convert the "key" to a byte for keybd_event?


Solution

  • Thank you David. Sory for the documentation, english is not my first langage and I don't understand all.

    So this code works fine :

    procedure TAutoComplete.AutoCompleteKeyPress(Sender: TObject; var Key: Char);
    var
        Input    : TInput;
        InputList: TList<TInput>;
    begin
        FXEdit.SetFocus;
    
        InputList := TList<TInput>.Create;
        try
            Input            := Default (TInput);
            Input.Itype      := INPUT_KEYBOARD;
            Input.ki.dwFlags := KEYEVENTF_UNICODE;
            Input.ki.wScan   := ord(Key);
            InputList.Add(Input);
            Input.ki.dwFlags := KEYEVENTF_UNICODE or KEYEVENTF_KEYUP;
            InputList.Add(Input);
    
            SendInput(InputList.Count, InputList.List[0], SizeOf(TInput));
        finally
            InputList.Free;
        end;
    end;