How to distinguish keyboard keys with the same program code? For example, the "up arrow" on a standard keyboard and the number 8 on the numeric keypad on the right ("up arrow too!"). Here I am processing the key code in the hook in two ways:

1 way <structure PMsg>

function KeyBoardProc(ACode: Integer; AWParam :WParam; AMsg: PMsg): LRESULT; stdcall; begin ... form1.caption:=Inttostr(AMsg.message); // Структура PMsg ... result:=CallNextHookEx(hh,ACode,AWParam,LongInt(Amsg)); end; 

2 way <remove (scan) the state of all virtual keys>

 function KeyBoardProc(ACode: Integer; AWParam :WParam; AMsg: PMsg): LRESULT; stdcall; var state:TKeyBoardState; begin GetKeyboardState(State); for i:=1 to 255 do if((State[i] and 128) <> 0) then form1.caption:=Inttostr(i); // получаем нажатую клавишу result:=CallNextHookEx(hh,ACode,AWParam,LongInt(Amsg)); end; 
  • Both those and others are guilty :-). And Rabinovich :-) - karmadro4

2 answers 2

Look for example here a table of key codes.

Up arrow:

Key Decimal Number Hexadecimal Symbolic Name

 курсор ↑ 38 0×26 VK_UP 

to numpad

 8 104 0×68 VK_NUMPAD8 

    See also MSDN "KeyboardProc callback function"

    In short: you need to check for "key expansion", this is the 24th bit in the lParam parameter, for some reason you have it named AMsg.