Hot Keys for your Application

Use this board to post your code snippets - tips and tricks
5 posts Page 1 of 1
Contributors
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Hot Keys for your Application
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..
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
tedhead2
Excellent Poster
Excellent Poster
Posts: 338
Joined: Sun Jan 03, 2010 8:36 am

Re: Hot Keys for your Application
tedhead2
"GetAsyncKeyState" Is not declared...
Image
User avatar
uNhoL
Just Registered
Just Registered
Posts: 1
Joined: Tue Apr 06, 2010 4:09 pm

Re: Hot Keys for your Application
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
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

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
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Hot Keys for your Application
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
5 posts Page 1 of 1
Return to “Quick Snips”