|
-
Feb 6th, 2007, 08:31 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED]Global Keypress?
I'm writing a program that sends keys on keypess. So if they press F2 it sends "Hello World" or whatever they want it to send. But I don't know how to detect global keystrokes. I have searched this forum and found that I need to use a hook but everything I found for it doesn't help me. Any help? Thanks
Last edited by bluehairman; Feb 12th, 2007 at 04:11 PM.
-
Feb 6th, 2007, 09:52 PM
#2
Member
Re: Global Keypress?
on your form turn on keypreview and then :
for keydown event:
nonnumberentered = False
If e.KeyCode = Keys.Delete Then
do something
End If
keypress event:
If nonnumberentered = True Then e.Handled = True
declare nonnumberentered
and done !
-
Feb 7th, 2007, 05:15 AM
#3
Re: Global Keypress?
 Originally Posted by bluehairman
I'm writing a program that sends keys on keypess. So if they press F2 it sends "Hello World" or whatever they want it to send. But I don't know how to detect global keystrokes. I have searched this forum and found that I need to use a hook but everything I found for it doesn't help me. Any help? Thanks
Hi,
Here's a link about the Keypress Event;
http://msdn2.microsoft.com/en-us/lib...ffice.10).aspx
Wkr,
sparrow1
-
Feb 7th, 2007, 03:58 PM
#4
Thread Starter
Hyperactive Member
Re: Global Keypress?
 Originally Posted by wrecklesswun
on your form turn on keypreview and then :
for keydown event:
nonnumberentered = False
If e.KeyCode = Keys.Delete Then
do something
End If
keypress event:
If nonnumberentered = True Then e.Handled = True
declare nonnumberentered
and done !
Thats not for global. If I'm wrong correct me. sparrow1 I'm checking out that page right now.
-
Feb 7th, 2007, 04:57 PM
#5
Thread Starter
Hyperactive Member
Re: Global Keypress?
Ok, I'm using VS 2005 and e.KeyCode isn't declaired. What else can I do?
-
Feb 7th, 2007, 06:03 PM
#6
Re: Global Keypress?
 Originally Posted by bluehairman
Ok, I'm using VS 2005 and e.KeyCode isn't declaired. What else can I do?
Hi,
Here's an example how to use Keypress:
VB Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control And e.KeyCode = Keys.D Then
MsgBox("You pushes the D key")
End If
If e.KeyCode = Keys.Delete Then
MsgBox("You pushes the Del key")
End If
End Sub
Wkr,
sparrow1
-
Feb 7th, 2007, 06:29 PM
#7
Thread Starter
Hyperactive Member
Re: Global Keypress?
VB Code:
'e.Control' is not a member of 'System.Windows.Forms.KeyPressEvenArgs'
'e.KeyCode' is not a member of 'System.Windows.Forms.KeyPressEvenArgs'
-
Feb 7th, 2007, 06:34 PM
#8
Thread Starter
Hyperactive Member
Re: Global Keypress?
Ok, I got it now, but theres still one problem. When I go into a different program and hit 'F2' nothing happens. Its still within the form.
-
Feb 7th, 2007, 06:37 PM
#9
Re: Global Keypress?
 Originally Posted by bluehairman
VB Code:
'e.Control' is not a member of 'System.Windows.Forms.KeyPressEvenArgs'
'e.KeyCode' is not a member of 'System.Windows.Forms.KeyPressEvenArgs'
Hi,
No keypress it is not a member of keypressEventArgs.
That's way you need the Keydown event for this like the code I gave you in the previous post.
Do the test and include my code into your form class.
The first test is the controlbutton and the key "D".
The second test just push the del button.
See what happens.
Wkr,
sparrow1
-
Feb 7th, 2007, 06:46 PM
#10
Thread Starter
Hyperactive Member
Re: Global Keypress?
VB Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F2 Then
SendKeys.Send(TextBox1.Text)
If CheckBox1.Checked = True Then
SendKeys.Send("{enter}")
End If
End If
End Sub
Thats my code. But it only works within the form. I want it to be globaly, so I can send stuff to other programs, web pages, ect.
-
Feb 8th, 2007, 03:01 AM
#11
Re: Global Keypress?
There is a system wide hotkey control here which uses the RegisterHotkey API call:
VB Code:
<DllImport("user32", EntryPoint:="RegisterHotKey", _
SetLastError:=True, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function RegisterHotkey(ByVal hwnd As IntPtr, _
ByVal Id As Int32, _
<MarshalAs(UnmanagedType.U4)> ByVal fsModifiers As Int32, _
<MarshalAs(UnmanagedType.U4)> ByVal vkey As Int32) As Boolean
End Function
-
Feb 8th, 2007, 07:49 PM
#12
Thread Starter
Hyperactive Member
Re: Global Keypress?
Ok, but how sould I get the F1, F2, F3, F4, ect. keys too. I read that site and I found this only:
VB Code:
The id parameter is used to differentiate between multiple hotkeys that can be received by a given window. To ensure that we are dealing with an unique ID, I use the API call GlobalAddAtom to return the id. This is declared thus:
<DllImport("kernel32", EntryPoint:="GlobalAddAtom", _
SetLastError:=True, _
ExactSpelling:=False)> _
Public Function GlobalAddAtom(<MarshalAs(UnmanagedType.LPTStr)> _
ByVal lpString As String) As Int32
End Function
But I don't know how I would add the other keys. I also found this :
VB Code:
Public Enum HotkeyModifierFlags
MOD_ALT = &H1
MOD_CONTROL = &H2
MOD_SHIFT = &H4
MOD_WIN = &H8
End Enum
-
Feb 9th, 2007, 04:07 PM
#13
Thread Starter
Hyperactive Member
-
Feb 9th, 2007, 06:13 PM
#14
Re: Global Keypress?
Put one instance of the control on your form for each key combination you want to trap and set the properties of each appropriately..
-
Feb 9th, 2007, 08:14 PM
#15
Thread Starter
Hyperactive Member
Re: Global Keypress?
Yes, but thats what I need help with. How would I set the key combination?
-
Feb 9th, 2007, 08:56 PM
#16
Hyperactive Member
Re: Global Keypress?
 Originally Posted by Merrion
There is a system wide hotkey control here which uses the RegisterHotkey API call:
VB Code:
<DllImport("user32", EntryPoint:="RegisterHotKey", _
SetLastError:=True, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function RegisterHotkey(ByVal hwnd As IntPtr, _
ByVal Id As Int32, _
<MarshalAs(UnmanagedType.U4)> ByVal fsModifiers As Int32, _
<MarshalAs(UnmanagedType.U4)> ByVal vkey As Int32) As Boolean
End Function
hmm never seen it done that way. I usually do it like this example
http://forums.microsoft.com/msdn/sho...28399&siteid=1
VB Code:
Public Class Form1
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
' Pressed the hotkey!
Debug.WriteLine(m.Msg.ToString & ":" & m.WParam.ToString & ":" & m.LParam.ToString)
End If
MyBase.WndProc(m) '<-- Never Ever Forget This!
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call UnregisterHotKey(Me.Handle, 9) '<-- Don't forget this
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call RegisterHotKey(Me.Handle, 9, 0, 44) '<-- Play with these settings
End Sub
End Class
-
Feb 10th, 2007, 06:47 AM
#17
Re: Global Keypress?
How would I set the key combination?
Select the component and bring up its properties (F4) in the form designer.
Set the properties: AltKey , ShiftKey, CtrlKey, WinKey and VKey to the combination you want to trigger the event.
For example to use Shift+Ctrl+S use:
AltKey=False
ShiftKey=True
CtrlKey=True
Winkey=False
VKey=vbKeyS
-
Feb 11th, 2007, 10:27 AM
#18
Thread Starter
Hyperactive Member
Re: Global Keypress?
Ok. For the keys I noticed they use
&H(number here)
And it represents a key. So how would I find out F1, F2, ect. is?
-
Feb 11th, 2007, 07:02 PM
#19
Hyperactive Member
Re: Global Keypress?
 Originally Posted by bluehairman
Ok. For the keys I noticed they use
&H(number here)
And it represents a key. So how would I find out F1, F2, ect. is?
http://www.programmersheaven.com/dow.../download.aspx
-
Feb 11th, 2007, 10:32 PM
#20
Thread Starter
Hyperactive Member
Re: Global Keypress?
Thanks but, the F1, F2, ect. are only listen in virtual key codes, and they don't work. Anymore help?
-
Feb 11th, 2007, 11:03 PM
#21
Re: Global Keypress?
Here is an example of a hook I use on the keyboard in one of my apps, I modified the isHooked function to give you an example of how to se it for your needs.
VB Code:
Imports System.Runtime.InteropServices
Imports System.Reflection
Module keyHook
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Integer) As Integer
Public Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
ByVal dwThreadId As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Public Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
' Low-Level Keyboard Constants
Private Const HC_ACTION As Integer = 0
Private Const LLKHF_EXTENDED As Integer = &H1
Private Const MOD_CONTROL As Long = &H2
Private Const LLKHF_INJECTED As Integer = &H10
Private Const LLKHF_ALTDOWN As Integer = &H20
Private Const LLKHF_UP As Integer = &H80
Private Const LLKHF_DOWN As Integer = &H81
Private Const VK_F1 As Integer = &H70 'F1 key
Private Const VK_F2 As Integer = &H71 'F2 key
Private Const VK_F3 As Integer = &H72 'F3 key
Private Const VK_F4 As Integer = &H73 'F4 key
Private Const VK_F5 As Integer = &H74 'F5 key
Private Const VK_F6 As Integer = &H75 'F6 key
Private Const VK_F7 As Integer = &H76 'F7 key
Private Const VK_F8 As Integer = &H77 'F8 key
Private Const VK_F9 As Integer = &H78 'F9 key
Private Const VK_F10 As Integer = &H79 'F10 key
Private Const VK_F11 As Integer = &H7A 'F11 key
Private Const VK_F12 As Integer = &H7B 'F12 key
Public Const VK_CONTROL = &H11 'Control
Public Const VK_PRIOR = &H21 'PAGE UP key
Public Const VK_NEXT = &H22 'Page DOWN key
Private Const WH_KEYBOARD_LL As Integer = &H13
Public KeyboardHandle As Integer
Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
If (Hookstruct.vkCode = VK_F1) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) _
And CBool(Hookstruct.flags And LLKHF_UP) Then
MessageBox.Show("Hello World")
Return True
ElseIf (Hookstruct.vkCode = VK_F1) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
Return True
End If
'Etc
End Function
Private Sub HookedState(ByVal Text As String)
End Sub
Public Function KeyboardCallback(ByVal Code As Integer, _
ByVal wParam As Integer, _
ByRef lParam As KBDLLHOOKSTRUCT) As Integer
If (Code = HC_ACTION) Then
Debug.WriteLine("Calling IsHooked")
If (IsHooked(lParam)) Then
Return 1
End If
End If
Return CallNextHookEx(KeyboardHandle, _
Code, wParam, lParam)
End Function
Public Delegate Function KeyboardHookDelegate( _
ByVal Code As Integer, _
ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
As Integer
<MarshalAs(UnmanagedType.FunctionPtr)> _
Private callback As KeyboardHookDelegate
Public Sub HookKeyboard()
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
KeyboardHandle = SetWindowsHookEx( _
WH_KEYBOARD_LL, callback, _
Marshal.GetHINSTANCE( _
[Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
Call CheckHooked()
End Sub
Public Sub CheckHooked()
If (Hooked()) Then
'MsgBox("Keyboard hooked")
Else
MsgBox("Keyboard hook failed: " & Err.LastDllError)
End If
End Sub
Private Function Hooked()
Hooked = KeyboardHandle <> 0
End Function
Public Sub UnhookKeyboard()
If (Hooked()) Then
Call UnhookWindowsHookEx(KeyboardHandle)
End If
End Sub
End Module
Last edited by bmahler; Feb 11th, 2007 at 11:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|