Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

In user defined Hotkey, PC registers 'Shift' as 'Alt' and vice versa


I Have set up a form to allow users to define and assign hotkeys for my application. It works, but I'm getting strange behaviour with the hotkey control in the following case:

If user selects Cntrl + a key as the hotkey, then the hotkey is correctly registered ie, when user presses Cntrl + a key after assigning, the hotkey is triggered.

HOWEVER, if user selects Alt + a key as the hotkey, pressing Alt + a key does nothing, but pressing Shift + a key works! Similarly, if user selects Shift + a key as the hotkey, pressing Shift + a key does nothing, but pressing Alt + a key works.

I believe I have correctly set up the constants properly as below:

Cntrl = 2
Shift = 4
Alt = 1

Why is the recognition of Alt and Shift being reversed by the system?? Does anyone know what's going on here and how to rectify it?


  • 2
1 Solution
Private Const MOD_SHIFT = &H4
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2

Private Function RegisterKey(keycode As Integer, shift As Integer) as Integer
    Dim ShiftKeys as Integer

    ShiftKeys = 0;
    if ((shift And vbShiftMask) <> 0) then ' Shift key is pressed
         ShiftKeys = ShiftKeys Or MOD_SHIFT
    End If
    if ((shift And VbCtrlMask) <> 0) then ' Ctrl key is pressed
         ShiftKeys = ShiftKeys Or MOD_CONTROL
    End If
    if ((shift And VbAltMask) <> 0) then ' Alt key is pressed
         ShiftKeys = ShiftKeys Or MOD_ALT
    End If

    RegisterKey = RegisterHotKey(Me.hwnd, 2, ShiftKeys, KeyCode)
End Sub

Good Luck!
yes, you are right.  alt and shift key values should be reversed....use the code similar to above.
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
DoctorNashAuthor Commented:
The basis of the code presented by supunr is sound.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now