Announcement

Collapse
No announcement yet.

Detecting Numeric Keypad Keys

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detecting Numeric Keypad Keys

    Is there a way to detect that the key pressed was on the numeric keypad? A hook works fine IF THE NUMLOCK IS ON but if it is OFF I get the same virtual-key code as the standard keypad.

    Example
    If the Numeric Keypad 0/Ins key is pressed with NumLock ON I get a key code of VK_NUMPAD0 ... Just what you would expect. But,if the Numeric Keypad 0/Ins key is pressed with NumLock OFF I get a key code of VK_INSERT.

    I have tried several different keyboards with the same result.

  • #2
    Larry --
    in KeyboardProc you receive wParam & lParam.
    To receive scan-code you need to analyze not wParam, but bits 16-23 of lParam.

    Scan-codes for numpad:

    Case &H45: ' NumLock
    Case &H47: ' Home / 7
    Case &H48: ' Up / 8
    Case &H49: ' PgUp / 9
    Case &H4A: ' NumPad -
    Case &H4B: ' Left / 4
    Case &H4C: ' Center / 5
    Case &H4D: ' Right / 6
    Case &H4E: ' Numpad +
    Case &H4F: ' End / 1
    Case &H50: ' Down / 2
    Case &H51: ' PgDn / 3
    Case &H52: ' Ins / 0
    Case &H53: ' Del / .

    [This message has been edited by Semen Matusovski (edited June 08, 2000).]

    Comment


    • #3
      Semen

      The problem is that the scan codes in lParam are NOT CORRECT if NumLock is NOT ON.

      ------------------


      [This message has been edited by Larry Westrick (edited June 08, 2000).]

      Comment


      • #4
        Larry -
        try this.
        On my PC results are the same for NumLock On & Off.
        Code:
           #Compile Exe
           #Register None
           #Dim All
           #Include "win32Api.inc"
           Global ghKbrdHook As Long, lpfnKbrdHook  As Long, hDlg As Long
           Function KeyboardHook(ByVal iCode As Integer, ByVal wParam As Long, ByVal lParam As Long) As Dword
              Local ScanCode As Long, t As String, Enhanced As Long
              If iCode = %HC_ACTION Then
                 If (lParam And &H80000000) = 0 Then ' Pressed
                    Enhanced = IsTrue(lParam And &H01000000)
                    ScanCode = Asc(Mkl$(lParam), 3)
                    Select Case ScanCode
                       Case &H52: t$ = "Numpad 0"
                       Case &H4F: t$ = "Numpad 1"
                       Case &H50: t$ = "Numpad 2"
                       Case &H51: t$ = "Numpad 3"
                       Case &H4B: t$ = "Numpad 4"
                       Case &H4C: t$ = "Numpad 5"
                       Case &H4D: t$ = "Numpad 6"
                       Case &H47: t$ = "Numpad 7"
                       Case &H48: t$ = "Numpad 8"
                       Case &H49: t$ = "Numpad 9"
                    End Select
                    If EnHanced Then t$ = ""
                    SetWindowText hDlg, ByCopy t$
                 End If
              End If
              Function = CallNextHookEx(ghKbrdHook, iCode, wParam, lParam)
           End Function
           CallBack Function DlgProc
              Select Case CbMsg
                 Case %WM_INITDIALOG
                    ghKbrdHook = SetWindowsHookEx(%WH_KEYBOARD, CodePtr(KeyboardHook), _
                       0, GetCurrentThreadId)
                 Case %WM_DESTROY: UnhookWindowsHookEx ghKbrdHook
              End Select
           End Function
           Function PbMain
              Dialog New 0,"Press keys",,, 100, 100, %WS_CAPTION Or %WS_SYSMENU To hdlg
              Dialog Show Modal hdlg Call DlgProc
           End Function
        [This message has been edited by Semen Matusovski (edited June 08, 2000).]

        Comment


        • #5
          Semen

          Thanks a lot. I tried it on several systems and they all work. I must have a problem with my computer. This system, doesn't matter what keyboard I use, will not give me the correct codes unless the NumLock is ON. I guess I'll have to swap it out.

          ------------------




          [This message has been edited by Larry Westrick (edited June 08, 2000).]

          Comment

          Working...
          X