Results 1 to 21 of 21

Thread: [RESOLVED]Global Keypress?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Resolved [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.

  2. #2
    Member
    Join Date
    Sep 2005
    Posts
    49

    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 !

  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Global Keypress?

    Quote 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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Global Keypress?

    Quote 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.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Global Keypress?

    Ok, I'm using VS 2005 and e.KeyCode isn't declaired. What else can I do?

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Global Keypress?

    Quote 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:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.         If e.Control And e.KeyCode = Keys.D Then
    3.             MsgBox("You pushes the D key")
    4.         End If
    5.         If e.KeyCode = Keys.Delete Then
    6.             MsgBox("You pushes the Del key")
    7.         End If
    8.     End Sub

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Global Keypress?

    VB Code:
    1. 'e.Control' is not a member of 'System.Windows.Forms.KeyPressEvenArgs'
    2. 'e.KeyCode' is not a member of 'System.Windows.Forms.KeyPressEvenArgs'

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    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.

  9. #9
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Global Keypress?

    Quote Originally Posted by bluehairman
    VB Code:
    1. 'e.Control' is not a member of 'System.Windows.Forms.KeyPressEvenArgs'
    2. '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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Global Keypress?

    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.         If e.KeyCode = Keys.F2 Then
    3.             SendKeys.Send(TextBox1.Text)
    4.             If CheckBox1.Checked = True Then
    5.                 SendKeys.Send("{enter}")
    6.             End If
    7.         End If
    8.     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.

  11. #11
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Global Keypress?

    There is a system wide hotkey control here which uses the RegisterHotkey API call:

    VB Code:
    1. <DllImport("user32", EntryPoint:="RegisterHotKey", _
    2.           SetLastError:=True, _
    3.           ExactSpelling:=True, _
    4.           CallingConvention:=CallingConvention.StdCall)> _
    5. Public Function RegisterHotkey(ByVal hwnd As IntPtr, _
    6.            ByVal Id As Int32, _
    7.            <MarshalAs(UnmanagedType.U4)> ByVal fsModifiers As Int32, _
    8.            <MarshalAs(UnmanagedType.U4)> ByVal vkey As Int32) As Boolean
    9.  
    10. End Function

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    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:
    1. 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:
    2.  
    3. <DllImport("kernel32", EntryPoint:="GlobalAddAtom", _
    4. SetLastError:=True, _
    5. ExactSpelling:=False)> _
    6. Public Function GlobalAddAtom(<MarshalAs(UnmanagedType.LPTStr)> _
    7.                 ByVal lpString As String) As Int32
    8.  
    9. End Function
    But I don't know how I would add the other keys. I also found this :
    VB Code:
    1. Public Enum HotkeyModifierFlags
    2.     MOD_ALT = &H1
    3.     MOD_CONTROL = &H2
    4.     MOD_SHIFT = &H4
    5.     MOD_WIN = &H8
    6. End Enum

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Global Keypress?

    Any help here? Thanks.

  14. #14
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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..

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Global Keypress?

    Yes, but thats what I need help with. How would I set the key combination?

  16. #16
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: Global Keypress?

    Quote Originally Posted by Merrion
    There is a system wide hotkey control here which uses the RegisterHotkey API call:

    VB Code:
    1. <DllImport("user32", EntryPoint:="RegisterHotKey", _
    2.           SetLastError:=True, _
    3.           ExactSpelling:=True, _
    4.           CallingConvention:=CallingConvention.StdCall)> _
    5. Public Function RegisterHotkey(ByVal hwnd As IntPtr, _
    6.            ByVal Id As Int32, _
    7.            <MarshalAs(UnmanagedType.U4)> ByVal fsModifiers As Int32, _
    8.            <MarshalAs(UnmanagedType.U4)> ByVal vkey As Int32) As Boolean
    9.  
    10. 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:
    1. Public Class Form1
    2.  
    3. Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    4. Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
    5. Public Const WM_HOTKEY As Integer = &H312
    6.  
    7. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    8.     If m.Msg = WM_HOTKEY Then
    9.         ' Pressed the hotkey!
    10.         Debug.WriteLine(m.Msg.ToString & ":" & m.WParam.ToString & ":" & m.LParam.ToString)
    11.     End If
    12.     MyBase.WndProc(m) '<-- Never Ever Forget This!
    13. End Sub
    14. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    15.     Call UnregisterHotKey(Me.Handle, 9) '<-- Don't forget this
    16. End Sub
    17.  
    18. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.     Call RegisterHotKey(Me.Handle, 9, 0, 44) '<-- Play with these settings
    20. End Sub
    21. End Class

  17. #17
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    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?

  19. #19
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: Global Keypress?

    Quote 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

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Global Keypress?

    Thanks but, the F1, F2, ect. are only listen in virtual key codes, and they don't work. Anymore help?

  21. #21
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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:
    1. Imports System.Runtime.InteropServices
    2. Imports System.Reflection
    3. Module keyHook
    4.  
    5.     Public Declare Function UnhookWindowsHookEx Lib "user32" _
    6.       (ByVal hHook As Integer) As Integer
    7.  
    8.     Public Declare Function SetWindowsHookEx Lib "user32" _
    9.       Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
    10.       ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
    11.       ByVal dwThreadId As Integer) As Integer
    12.  
    13.     Private Declare Function GetAsyncKeyState Lib "user32" _
    14.       (ByVal vKey As Integer) As Integer
    15.  
    16.     Private Declare Function CallNextHookEx Lib "user32" _
    17.       (ByVal hHook As Integer, _
    18.       ByVal nCode As Integer, _
    19.       ByVal wParam As Integer, _
    20.       ByVal lParam As KBDLLHOOKSTRUCT) As Integer
    21.  
    22.     Public Structure KBDLLHOOKSTRUCT
    23.         Public vkCode As Integer
    24.         Public scanCode As Integer
    25.         Public flags As Integer
    26.         Public time As Integer
    27.         Public dwExtraInfo As Integer
    28.     End Structure
    29.  
    30.     ' Low-Level Keyboard Constants
    31.     Private Const HC_ACTION As Integer = 0
    32.     Private Const LLKHF_EXTENDED As Integer = &H1
    33.     Private Const MOD_CONTROL As Long = &H2
    34.     Private Const LLKHF_INJECTED As Integer = &H10
    35.     Private Const LLKHF_ALTDOWN As Integer = &H20
    36.     Private Const LLKHF_UP As Integer = &H80
    37.     Private Const LLKHF_DOWN As Integer = &H81
    38.  
    39.     Private Const VK_F1 As Integer = &H70   'F1 key
    40.     Private Const VK_F2 As Integer = &H71   'F2 key
    41.     Private Const VK_F3 As Integer = &H72   'F3 key
    42.     Private Const VK_F4 As Integer = &H73   'F4 key
    43.     Private Const VK_F5 As Integer = &H74   'F5 key
    44.     Private Const VK_F6 As Integer = &H75   'F6 key
    45.     Private Const VK_F7 As Integer = &H76   'F7 key
    46.     Private Const VK_F8 As Integer = &H77   'F8 key
    47.     Private Const VK_F9 As Integer = &H78   'F9 key
    48.     Private Const VK_F10 As Integer = &H79  'F10 key
    49.     Private Const VK_F11 As Integer = &H7A  'F11 key
    50.     Private Const VK_F12 As Integer = &H7B   'F12 key
    51.  
    52.  
    53.     Public Const VK_CONTROL = &H11  'Control
    54.     Public Const VK_PRIOR = &H21    'PAGE UP key
    55.     Public Const VK_NEXT = &H22     'Page DOWN key
    56.  
    57.     Private Const WH_KEYBOARD_LL As Integer = &H13
    58.     Public KeyboardHandle As Integer
    59.  
    60.  
    61.     Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
    62.  
    63.         If (Hookstruct.vkCode = VK_F1) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) _
    64.             And CBool(Hookstruct.flags And LLKHF_UP) Then
    65.             MessageBox.Show("Hello World")
    66.             Return True
    67.         ElseIf (Hookstruct.vkCode = VK_F1) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
    68.             Return True
    69.         End If
    70.  
    71.         'Etc
    72.     End Function
    73.  
    74.     Private Sub HookedState(ByVal Text As String)
    75.  
    76.     End Sub
    77.  
    78.     Public Function KeyboardCallback(ByVal Code As Integer, _
    79.       ByVal wParam As Integer, _
    80.       ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    81.  
    82.         If (Code = HC_ACTION) Then
    83.             Debug.WriteLine("Calling IsHooked")
    84.  
    85.             If (IsHooked(lParam)) Then
    86.                 Return 1
    87.             End If
    88.  
    89.         End If
    90.  
    91.         Return CallNextHookEx(KeyboardHandle, _
    92.           Code, wParam, lParam)
    93.  
    94.     End Function
    95.  
    96.  
    97.     Public Delegate Function KeyboardHookDelegate( _
    98.       ByVal Code As Integer, _
    99.       ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
    100.                    As Integer
    101.  
    102.     <MarshalAs(UnmanagedType.FunctionPtr)> _
    103.     Private callback As KeyboardHookDelegate
    104.  
    105.     Public Sub HookKeyboard()
    106.         callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    107.  
    108.         KeyboardHandle = SetWindowsHookEx( _
    109.           WH_KEYBOARD_LL, callback, _
    110.           Marshal.GetHINSTANCE( _
    111.           [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
    112.  
    113.         Call CheckHooked()
    114.     End Sub
    115.  
    116.     Public Sub CheckHooked()
    117.         If (Hooked()) Then
    118.             'MsgBox("Keyboard hooked")
    119.         Else
    120.             MsgBox("Keyboard hook failed: " & Err.LastDllError)
    121.         End If
    122.     End Sub
    123.  
    124.     Private Function Hooked()
    125.         Hooked = KeyboardHandle <> 0
    126.     End Function
    127.  
    128.     Public Sub UnhookKeyboard()
    129.         If (Hooked()) Then
    130.             Call UnhookWindowsHookEx(KeyboardHandle)
    131.         End If
    132.     End Sub
    133.  
    134. End Module
    Last edited by bmahler; Feb 11th, 2007 at 11:08 PM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width