Link to home
Start Free TrialLog in
Avatar of DoctorNash
DoctorNash

asked on

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

Experts,

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?

Thanx

Avatar of supunr
supunr

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!
ASKER CERTIFIED SOLUTION
Avatar of supunr
supunr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DoctorNash:
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?
Avatar of DoctorNash

ASKER

The basis of the code presented by supunr is sound.