Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > [VB.2010] Problem with Key-Hook


Reply
 
Thread Tools Display Modes
  #1  
Old 04-25-2012, 01:35 PM
lack lack is offline
Newcomer
 
Join Date: Apr 2012
Posts: 4
Default [VB.2010] Problem with Key-Hook


Hello,
I have a little problem with this source code.
It should spam "bla" when Ctrl + 1 is pressed


Code:
Public Class frmKeytest
    Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vk As Integer) As Integer
    Private Declare Sub UnregisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer)
    Private Const Key_NONE As Integer = &H0
    Private Const WM_HOTKEY As Integer = &H312
    Private Const MOD_ALT As Integer = &H1
    Private Const MOD_CONTROL As Integer = &H2
    Private Const MOD_SHIFT As Integer = &H4
    Private Const MOD_WIN As Integer = &H8

    Protected Overrides Sub WndProc(ByRef m As Message)
        'die messages auswerten
        If m.Msg = WM_HOTKEY Then
            Select Case m.WParam
                Case 1
                    MessageBox.Show("you pressed Tab")
                Case 2
                    SendKeys.Send("bla")
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

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

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 1, Key_NONE, Keys.Tab)
        RegisterHotKey(Me.Handle, 2, MOD_CONTROL, Keys.D1)
    End Sub
End Class
Reply With Quote
  #2  
Old 04-25-2012, 04:31 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,892
Default

You're not checking the return value to see if Ctrl-1 is already registered.
MSDN:"RegisterHotKey fails if the keystrokes specified for the hot key have already been registered by another hot key."
The code worked fine for me as written.
If I bring up notepad, then press Ctrl-1, "bla" is "typed" into notepad.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 04-25-2012 at 04:39 PM.
Reply With Quote
  #3  
Old 04-26-2012, 02:33 AM
lack lack is offline
Newcomer
 
Join Date: Apr 2012
Posts: 4
Default

this function works for me too but except for Ctrl + "" hotkeys
it types once "bla" but then sendkeys doesn't work, I have to restart the program
I also tried to use a msgbox instead of sendkeys and this worked fine
but sendkeys still fails..
I think the notepad window is confused because Ctrl is pressed and it only gets
these keys: ctrl+b , ctrl+l , ctrl+a
Reply With Quote
  #4  
Old 04-27-2012, 03:46 AM
lack lack is offline
Newcomer
 
Join Date: Apr 2012
Posts: 4
Default

/push
still have this problem...
Reply With Quote
  #5  
Old 05-06-2012, 12:39 PM
lack lack is offline
Newcomer
 
Join Date: Apr 2012
Posts: 4
Default

/psuh
please help me!
Reply With Quote
Reply

Tags
hook, keyboard


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->