Today I found that Delphi doesn't distinguish between some extended keys and the numlock keys (e.g. Ctrl + Left = Ctrl + Num 4) and shows the wrong keyboard shortcuts in the THotKey control. (as answered by Ondrej Kelle​ in the linked StackOverflow question).

Today I found that Delphi doesn't distinguish between some extended keys and the numlock keys (e.g. Ctrl + Left = Ctrl + Num 4) and shows the wrong keyboard shortcuts in the THotKey control. (as answered by Ondrej Kelle​ in the linked StackOverflow question).

I came up with the following helper procedure for setting the THotkey.Hotkey property:

procedure THotkey_SetHotkey(_hk: THotKey; _Shortcut: TShortcut);
var
  Key: Word;
  Shift: TShiftState;
begin
  ShortCutToKey(_Shortcut, Key, Shift);
  _hk.HotKey := _Shortcut;
  // If it is an "extended" key, we need to set the hkExt flag in Modifiers
  // Extended keys are
  // * left/right/up/down cursor keys
  // * PageUp/Down, Home/End, Delete/Insert keys
  // * Numlock, Break and Print keys
  // * Divide and Enter key (there is no special key code for Enter,
  // so we have to ignore it
  if Key in [VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN,
    VK_PRIOR, VK_NEXT, VK_HOME, VK_END, VK_INSERT, VK_DELETE,
    VK_NUMLOCK, VK_PAUSE, VK_PRINT,
    VK_DIVIDE] then
    _hk.Modifiers := _hk.Modifiers + [hkExt];
end;

Do you see anything wrong with this? Or is there already a workaround for this problem in the Delphi RTL of which I am just not aware? Have you solved that problem differently?
http://stackoverflow.com/a/3108718/49925

Comments