?
Solved

Re. Hotkey Creation - Simple Question but Urgent Answer needed

Posted on 2003-03-04
6
Medium Priority
?
363 Views
Last Modified: 2008-02-01
I've got a feeling this is not too difficult, but it's got me stumped.
I have 2 pieces of code that work for me, but work independently as follows:

1. http://www.mvps.org/ccrp/controls/ccrphotkey6.htm
This allows a user to press a combination of keys and have the control display the chosen key combination, converting it to a KeyCode and Shift combination

2. http://www.codeguru.com/vb/articles/2167.shtml
This allows Multiple HotKeys to be set up in a single
application, with each key assigned a different function.

Now, the issue is that in 2. above the HotKeys to be set up are 'hard-coded' for example:
retVal0 = RegisterHotKey(Me.hwnd, 0, MOD_CTRL, VK_F10)
' This sets up HotKey CTRL+F10  
retVal1 = RegisterHotKey(Me.hwnd, 1, MOD_CTRL, VK_F11)
'This sets up HotKey CTRL+F11  
retVal2 = RegisterHotKey(Me.hwnd, 2, MOD_CTRL, VK_F12)
'This sets up HotKey CTRL+F12  

BUT, I want to be able to register a user-selected HotKey (ie to use any HotKey selected by user in code 1. above). How would I reference the user-selected HotKey in the RegisterHotKey commands in code 2.?

When I press CTRL F10 in 1. above, it returns 'Keycode: 121 Shift: 2'. What does that mean? How does this Keycode and Shift value relate to 'MOD_CTRL' and 'VK_F10' in 2.? What is the translation?

Hope this makes sense.
Thanks!
0
Comment
Question by:DoctorNash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:supunr
ID: 8069031
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
0
 
LVL 11

Accepted Solution

by:
supunr earned 160 total points
ID: 8069054
You might also want to register only perticular keys.  If that is the case add this select case:

select case KeyCode
  Case vbKeyA to vbKeyZ:
  Case vbKey0 to vbKey9:
  case Asc("a") to Asc("a"):
  case vkKeyF1 to vbKeyF12:
  Case Else:
    KeyCode = 0
end Select

if (KeyCode > 0) Then
RegisterKey = RegisterHotKey(Me.hwnd, 2, ShiftKeys, KeyCode)
End if
0
 
LVL 9

Expert Comment

by:Naveen Swamy
ID: 8069948
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DoctorNash
ID: 8075913
Supunr,
Thanks for your response. You have been most helpful. So, just to confirm, it appears that it would be true to say that:

If Cntrl F10 translates to Shift=2 and Keycode = 121
then the following statement is valid:

RegisterKey=RegisterHotKey(Me.hwnd, 2, 2, 121)

Is this correct?
0
 
LVL 11

Expert Comment

by:supunr
ID: 8076136
Thanks for the points.  Yes, it translate to what you have mensioned.  Say if you have Ctrl+Shift F10, then it will be

RegisterKey=RegisterHotKey(Me.hwnd, 2, 6, 121)

where 6 = MOD_SHIFT  OR MOD_CONTROL ' Bitwise or (in this case 4+2)
0
 

Author Comment

by:DoctorNash
ID: 8149160
Experts,

Have set up my user defined hotkeys. It works, but am 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, the hotkey is triggered. HOWEVER, if user selects Alt + a key, or Shift + a key, the reciprocal hotkey to the one expected gets registered.

ie user thinks he has registered Alt + a key, but hotkey actually triggers when user presses Shift + a key, NOT Alt + a key.

Similarly, user thinks he has registered Shift + a key, but hotkey actually triggers when user presses Alt + a key NOT Shift + a key.

I believe I have correctly set up the constants as shown earlier in this thread ie

Cntrl = 2
Shift = 4
Alt = 1


Why is the recognition of Alt and Shift being reversed by the system?? What's going on here?

Thanx
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question