Author: Lou Adler
Does anyone know how to get the name of a key from the keyboard? I want a method
that sends the ASCII code and returns the correct name in string format.
Answer:
Start with calling VkKeyScan, then proceed to GetKeynameText, via MapVirtualKey.
1 function GetKeyname(ch: Char): string;
2 var3 scan: Word;
4 virtual_keycode: Byte;
5 keyname: array[0..128] of Char;
6 lparam: Integer;
7 begin8 scan := VkKeyScan(ch);
9 Result := '';
10 if scan <> $FFFF then11 begin12 if (Scan and $100) <> 0 then13 Result := Result + '[Shift]';
14 if (Scan and $200) <> 0 then15 Result := Result + '[Ctrl]';
16 if (Scan and $400) <> 0 then17 Result := Result + '[Alt]';
18 virtual_keycode := Lobyte(scan);
19 lparam := MapVirtualKey(virtual_keycode, 0) shl 16;
20 if lparam <> 0 then21 if GetKeyNametext(lparam, keyname, sizeof(keyname)) > 0 then22 Result := Result + '[' + keyname + ']';
23 end;
24 end;
25 26 procedure TForm1.Button1Click(Sender: TObject);
27 begin28 memo1.text := GetKeyname(alignededit1.text[1]);
29 end;