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

x
?
Solved

Re. Hotkey Creation - Simple Question but Urgent Answer needed

Posted on 2003-03-04
6
Medium Priority
?
364 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

564 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