Hot Keys for your Application
Use this board to post your code snippets - tips and tricks
5 posts
Page 1 of 1
You will need the following:
1 Timer
The Code:
Ex:
1 Timer
The Code:
Code: Select all
In all instances where it says "Keys.KEY TO BE PRESSED" change KEY TO BE PRESSED to a key.'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
Ex:
Code: Select all
or:
Keys.F1
Code: Select all
and so on..Keys.F2
<?php
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
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
to your code and then you can use it like <DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function GetAsyncKeyState(ByVal vkey As Integer) As Short
End Function
Code: Select all
just for an example If GetAsyncKeyState(Keys.F9) Then
MsgBox("Testing Message", MsgBoxStyle.OkOnly, "Testing Title")
End If
If anyone is having any problems with this you can check my video tutorial.
Hotkey Code:
Checked MenuStrip Code:
Code: Select all
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. 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
Checked MenuStrip Code:
Code: Select all
Unchecked MenuStrip Code:
PinToolStripMenuItem.Checked = True
PinToolStripMenuItem.CheckState = CheckState.Checked
Code: Select all
PinToolStripMenuItem.Checked = False
PinToolStripMenuItem.CheckState = CheckState.Unchecked
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023