Page 1 of 1

Hot Keys for your Application

Posted: Wed Feb 03, 2010 12:30 am
by Alex
You will need the following:
1 Timer

The Code:
Code: Select all
'Set "KEY TO BE PRESSED" to the key you want to be pressed as the hotkey.
 Dim HOTKEY1 As Boolean = GetAsyncKeyState(Keys.KEY TO BE PRESSED)
        If HOTKEY1 = True Then
           'Insert what you want your application to do when they hotkey is pressed.
        End If
        Dim HOTKEY2 As Boolean = GetAsyncKeyState(Keys.KEY TO BE PRESSED)
        If HOTKEY2 = True Then
             'Insert what you want your application to do when they hotkey is pressed.
        End If
In all instances where it says "Keys.KEY TO BE PRESSED" change KEY TO BE PRESSED to a key.
Ex:
Code: Select all
Keys.F1
or:
Code: Select all
Keys.F2
and so on..

Re: Hot Keys for your Application

Posted: Thu Mar 04, 2010 10:43 am
by tedhead2
"GetAsyncKeyState" Is not declared...

Re: Hot Keys for your Application

Posted: Tue Apr 06, 2010 4:59 pm
by uNhoL
tedhead2 wrote:
"GetAsyncKeyState" Is not declared...
Try Google.com

There you go:
http://msdn.microsoft.com/en-us/library ... S.85).aspx

Just add
Code: Select all
    <DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function GetAsyncKeyState(ByVal vkey As Integer) As Short
    End Function
to your code and then you can use it like
Code: Select all
        If GetAsyncKeyState(Keys.F9) Then
            MsgBox("Testing Message", MsgBoxStyle.OkOnly, "Testing Title")
        End If
just for an example

Re: Hot Keys for your Application

Posted: Tue Apr 06, 2010 6:17 pm
by mikethedj4
If anyone is having any problems with this you can check my video tutorial.
Hotkey Code:
Code: Select all
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
KeyPreview = True 
End Sub 

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown 
If e.KeyCode = Keys.Escape Then 
Me.Close() 
End If 
End Sub 
NOTE: Make sure you apply the hotkey to the button, or menu as well, while applying this code, otherwise it'll only work as a hotkey.

Checked MenuStrip Code:
Code: Select all
PinToolStripMenuItem.Checked = True 
PinToolStripMenuItem.CheckState = CheckState.Checked 
Unchecked MenuStrip Code:
Code: Select all
PinToolStripMenuItem.Checked = False 
PinToolStripMenuItem.CheckState = CheckState.Unchecked

Re: Hot Keys for your Application

Posted: Fri Apr 30, 2010 5:18 pm
by mandai
You might also want to check out the RegisterHotKey and UnregisterHotKey functions.
Code: Select all
<DllImport("user32.dll")> Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As UInteger, ByVal vk As UInteger) As Boolean
End Function

<DllImport("user32.dll")> Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
End Function

Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click
       Dim modifiers As UInteger = 0
       'modifiers += 1 = alt
       '+= 2 = ctrl
       '+= 4 = shift
       '+= 8 = win
       'can be added 2x maximum; e.g. 3 for ctrl + alt
       RegisterHotKey(Me.Handle, 0, modifiers, Keys.A)
End Sub

Const WM_HOTKEY As Integer = &H312

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
      If m.Msg = WM_HOTKEY Then
          MsgBox("hotkey with ID " & m.WParam.ToInt32() & " pressed")
      End If
      MyBase.WndProc(m)
End Sub

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
       UnregisterHotKey(Me.Handle, 0)
End Sub