Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello everyone. I am having a little trouble getting a custom keyboard shortcut made for my program. I have 2 forms (frmMain and frmSelect). I am trying to get a shortcut of CRTL+S to open up frmSelect from frmMain.

This is what I thought I would try, but the form is not opening up when I press CRTL and S.

Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.ControlKey + Keys.S Then
            frmSearch.Show()
        End If
    End Sub


Any help is much appreciated....
Posted
Updated 17-Jul-21 9:03am
v2
Comments
Member 15627495 10-Jul-22 1:38am    
System.Windows.Forms.KeyPressEventArgs

KeyPressEventArgs


sometimes event must be so accurate written

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keypresseventargs?view=windowsdesktop-6.0

A crude but effective method is to create a menu bar, set its Visible property to False. Then, add all your commands, write code for them and assign the desired shortcuts.
 
Share this answer
 
It has to do with the handling of the codes. If you added "Keys.S", which is given a value of 83 to "ControlKey", which has a value of 17. You would end up with 100, which corresponds to "4" on the Number Pad. If you hit "4" on the numpad, your second form will open.

To keep this from happening, try this instead. By checking the boolean values, you make sure that the keycode is what you meant it to be.

VB
Private Sub frmMain_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.S And e.Control = True Then
        frmSearch.Show()
    End If
End Sub
 
Share this answer
 
v3
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    'Trace Control + Key Event in VB6.0 
    '===========================================================
    If KeyCode = Asc("F") And Shift = 2 Then
        Call Command8_Click
    ElseIf KeyCode = Asc("D") And Shift = 2 Then
        Call Command9_Click
    ElseIf KeyCode = Asc("M") And Shift = 2 Then
        Call Command10_Click
    ElseIf KeyCode = Asc("P") And Shift = 2 Then
        Call Command11_Click
    ElseIf KeyCode = Asc("M") And Shift = 2 Then
        Call Check1_Click
    End If
    '===========================================================
End Sub




'VALUES FOR Shift  KEYS
'=====================================================
				  'SHIFT KEY = 1	
				  'CTRL KEY = 2
				  'CTRL + SHIFT KEY = 3
				  'ALT KEY = 4
				  'ALT+SHIFT KEY = 5
				  'CTRL+ALT KEY = 6
				  'ATRL+SHIFT+ALTER KEY = 7
 
Share this answer
 
v4
Comments
Richard MacCutchan 4-Jan-17 6:05am    
This question is more than 5 years old!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900