Google
 

Monday, August 30, 2004

List all the virtual key codes

This code snippet describes how to list all the virtual key codes and use them to simulate keyboard events. (email: snehanshu.ashar@acs-inc.com)

Description: Here, on formcreate, the combobox is built up with all key names using the GetKeyNameText function. I have added the virtual key code in the combo text. Then on combobox's change, you can simulate your Keybd_Event.

procedure TForm1.FormCreate(Sender: TObject);
var
i, j: Word;
MyKey: LPARAM; //Just to be brave!
TmpStr: array[0..100] of char;
begin
ComboBox1.Items.Clear;
ComboBox1.Text := '';
for i := 0 to 255 do
begin
MyKey := MapVirtualKey(i, 0); //if this is not done, we get
//actual keyboard scan code
MyKey := MyKey shl 16; //We need to shift it to left because the
// GetKeyNameText function expects it that way!
j := GetKeyNameText(MyKey, @TmpStr, 101);
if j > 0 then
begin
ComboBox1.Items.Add(string(TmpStr) + Format(' %.3d', [i]));
end;
end;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
var
VKey: Word;
begin
VKey := StrToInt(Copy(ComboBox1.Text, length(ComboBox1.Text) - 1, 3));
Edit1.SetFocus;
Keybd_Event(VKey, 0, 0, 0);
end;

No comments: