小白自製的快捷鍵轉碼
使用範例如下
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var s: string;
begin
s := HotKeyToHKName(Key, Shift);
Caption := s + ' ; ' + HKNameToCaption(s);
end;
代碼如下
(* ---------------------------------------------------------------------------------------------- *)
// XBHotKey
// Author: Kamilia
// 用途:熱鍵轉換及顯示
// 初版: 2019/5/1
(* ---------------------------------------------------------------------------------------------- *)
unit XBHotKey;
interface
uses System.Classes, System.SysUtils;
function HotKeyToHKName(Key: Word; Shift: TShiftState): string;
function HKNameToCaption(HKName: string): string;
implementation
(* ---------------------------------------------------------------------------------------------- *)
// KeyDown時 將使用者按下按鍵為HKName
(* ---------------------------------------------------------------------------------------------- *)
function HotKeyToHKName(Key: Word; Shift: TShiftState): string;
var SS: string;
begin
Result := inttostr(Key);
if ssShift in Shift then
SS := SS + 'S';
if ssAlt in Shift then
SS := SS + 'A';
if ssCtrl in Shift then
SS := SS + 'C';
if SS <> '' then
Result := Result + '_' + SS;
end;
(* ---------------------------------------------------------------------------------------------- *)
// HKName轉換為使用者可辨識的文字
(* ---------------------------------------------------------------------------------------------- *)
function HKNameToCaption(HKName: string): string;
var
S, KeyCaption: string;
_P: integer;
Key: Word;
i: integer;
begin
//先抓複合鍵 只留下非複合鍵
_P := Pos('_', HKName);
if _P > 0 then begin
S := Copy(HKName, _P + 1, Length(HKName) - _P);
HKName := Copy(HKName, 1, _P -1);
for I := 1 to Length(s) do
case S[i] of
'A': Result := Result + 'Alt + ';
'S': Result := Result + 'Shift + ';
'C': Result := Result + 'Ctrl + ';
end;
end;
Key := StrToIntDef(HKName, 0);
case Key of
8: KeyCaption := 'BackSpace';
13: KeyCaption := 'Enter';
19: KeyCaption := 'PauseBreak';
20: KeyCaption := 'CapsLock';
27: KeyCaption := 'Esc';
32: KeyCaption := '空白鍵';
33: KeyCaption := 'PageUp';
34: KeyCaption := 'PageDown';
35: KeyCaption := 'End';
36: KeyCaption := 'Home';
37: KeyCaption := '←';
38: KeyCaption := '↑';
39: KeyCaption := '→';
40: KeyCaption := '↓';
45: KeyCaption := 'Insert';
46: KeyCaption := 'Delete';
65..90: KeyCaption := Chr(Key); //英文字母
48..57: KeyCaption := '(左)' + inttostr(Key - 48); //左邊數字鍵
93: KeyCaption := 'Menu';
96..105: KeyCaption := '(右)' + inttostr(Key - 96); //左邊數字鍵
107: KeyCaption := '(右)*';
106: KeyCaption := '(右)+';
109: KeyCaption := '(右)-';
110: KeyCaption := '(右)小數點';
111: KeyCaption := '(右)/';
112..123: KeyCaption := 'F' + inttostr(Key - 111);
144: KeyCaption := 'NumLock';
145: KeyCaption := 'ScrollLock';
186: KeyCaption := ';';
187: KeyCaption := '=';
188: KeyCaption := ',';
189: KeyCaption := '-';
190: KeyCaption := '.';
191: KeyCaption := '/';
192: KeyCaption := '`';
219: KeyCaption := '[';
220: KeyCaption := '\';
221: KeyCaption := ']';
222: KeyCaption := '''';
16: KeyCaption := 'Shift';
17: KeyCaption := 'Ctrl';
18: KeyCaption := 'Alt';
else KeyCaption := 'Unknow(' + IntToStr(Key) + ')';
end;
if Key in [16..18] then begin
Result := KeyCaption;
Exit;
end;
Result := Result + KeyCaption;
end;
end.