Results 1 to 8 of 8

Thread: [RESOLVED] Detect keyboard keydown event anywhere on screen, inside or outside project

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    277

    Resolved [RESOLVED] Detect keyboard keydown event anywhere on screen, inside or outside project

    Hello everyone,

    I am trying to Detect both mouse click and keyboard keydown event anywhere on screen, inside or outside project. Google helps me to find mouse clicks anywhere on screen, but I couldn't find any solution to detect keyboard clicks.

    Code:
     Private Sub FrmSelectTask_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            TimerMouseDetect.Start()
            TimerMouseDetect.Interval = 100
    
        End Sub
    Code:
    Private Sub TimerMouseDetect_Tick(sender As Object, e As EventArgs) Handles TimerMouseDetect.Tick
    
            If MouseButtons = Windows.Forms.MouseButtons.Left Then
                MouseClickCounts = MouseClickCounts + 1
            End If
    
            If MouseButtons = Windows.Forms.MouseButtons.Right Then
                MouseClickCounts = MouseClickCounts + 1
            End If
    
        End Sub
    Anyone can help me to count keyboards clicks anywhere on screen

    Thank you
    Ladak

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,962

    Re: Detect keyboard keydown event anywhere on screen, inside or outside project

    Look into Hooking inputs

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    622

    Re: Detect keyboard keydown event anywhere on screen, inside or outside project

    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    277

    Re: Detect keyboard keydown event anywhere on screen, inside or outside project

    Thanks for your help & reply

    I added a new Class From : File > add > New project > Class Library

    Code:
    Imports System.Runtime.InteropServices
    Imports System.Reflection
    Imports System.Threading
    Imports System.Windows.Forms
    
    Public Class KeyboardLowLevelHook
        Implements IDisposable
    
        Private Declare Function UnhookWindowsHookEx Lib "user32" _
          (ByVal hHook As Integer) As Integer
    
        Private 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
    
        Private 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 LLKHF_INJECTED As Integer = &H10
        Private Const LLKHF_ALTDOWN As Integer = &H20
        Private Const LLKHF_UP As Integer = &H80
    
        'KeyUp/KeyDown constants
        Private Const WM_KEYDOWN As Integer = &H100
        Private Const WM_KEYUP As Integer = &H101
        Private Const WM_SYSKEYDOWN As Integer = &H104
        Private Const WM_SYSKEYUP As Integer = &H105
    
        Private Const WH_KEYBOARD_LL As Integer = 13&
        Private KeyboardHandle As Integer
        Private disposed As Boolean = False
    
        Private Delegate Function KeyboardHookDelegate( _
          ByVal Code As Integer, _
          ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
                       As Integer
    
        'Our keydown and keyup events
        Public Event KeyDown As KeyEventHandler
        Public Event KeyUp As KeyEventHandler
    
        <MarshalAs(UnmanagedType.FunctionPtr)> _
        Private callback As KeyboardHookDelegate = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    
        'Keyboard callback function
        Private Function KeyboardCallback(ByVal Code As Integer, _
          ByVal wParam As Integer, _
          ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    
            If (Code = HC_ACTION) Then
                'Call the appropriate event
    
                Dim KeyData As Keys
                Dim args As KeyEventArgs
    
                KeyData = lParam.vkCode
    
                'Check if the ALT key is pressed
                If CBool(GetAsyncKeyState(Keys.Menu) And &H8000) Then
                    'Alt key pressed
                    KeyData = KeyData Or Keys.Alt
                End If
    
                'Check if the Ctrl key is pressed
                If CBool(GetAsyncKeyState(Keys.ControlKey) And &H8000) Then
                    'Ctrl key pressed
                    KeyData = KeyData Or Keys.Control
                End If
    
                'Check if the Shift key was pressed
                If CBool(GetAsyncKeyState(Keys.ShiftKey) And &H8000) Then
                    'Shift key pressed
                    KeyData = KeyData Or Keys.Shift
                End If
    
                'Create the event args
                args = New KeyEventArgs(KeyData)
    
                'Raise the appropriate event
                If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
                    OnKeyDown(Me, args)
                ElseIf wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
                    OnKeyUp(Me, args)
                End If
    
                'Determine if we should block the key
                If args.Handled Then
                    Return 1
                End If
            End If
    
            'Call the next hook in the hook chain and return the value
            Return CallNextHookEx(KeyboardHandle, _
              Code, wParam, lParam)
    
        End Function
    
        Protected Sub OnKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            RaiseEvent KeyDown(sender, e)
        End Sub
    
        Protected Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
            RaiseEvent KeyUp(sender, e)
        End Sub
    
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            If Not disposed Then
                disposed = True
    
                'Unhook the keyboard
                UnhookWindowsHookEx(KeyboardHandle)
            Else
                Throw New ObjectDisposedException("KeyboardHook")
            End If
        End Sub
    
        Public Sub New()
            'Create a keyboard low-level hook
    
            KeyboardHandle = SetWindowsHookEx( _
                WH_KEYBOARD_LL, callback, _
                Marshal.GetHINSTANCE( _
                [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
        End Sub
    
        Protected Overrides Sub Finalize()
            MyBase.Finalize()
    
            If Not disposed Then
                Dispose()
            End If
        End Sub
    End Class

    Then I Added a reference to my solution.

    via project > add reference ->KBHook

    Next I Imported it in my project

    Code:
    Imports KBHook
    Also added this inside the class to use it

    Code:
        Private WithEvents KeyboardLowLevelHook As New KBHook.KeyboardLowLevelHook
    
        Private Sub m_Hook_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles KeyboardLowLevelHook.KeyDown
    
            If e.KeyCode = Keys.Escape Then
                MsgBox("Escape pressed")
            End If
    
        End Sub
    Everything goes fine, no error, when I run the project it does not detect escape key, I don't understand why this is so?

    Can someone please help me to detect the escape key outside my project?

    Thank you
    Ladak

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    622

    Re: Detect keyboard keydown event anywhere on screen, inside or outside project

    did you try to put the excape key in the KeyboardCallback function ?
    have a look here , it may gives you some clue : https://www.dreamincode.net/forums/t...22-escape-key/
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    277

    Re: Detect keyboard keydown event anywhere on screen, inside or outside project

    @Delaney.

    Thanks for your reply.

    The link you provided https://www.dreamincode.net/forums/t...22-escape-key/ helps me a lot and my escape key detection work is now working perfectly.

    Can you please check in my post #1 to detect mouse click is a professional work or it can create any issues on long terms basis?
    if you suggest to change please share some post for Hooking mouse in vb.net

    Thank you
    Ladak

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    622

    Re: Detect keyboard keydown event anywhere on screen, inside or outside project

    Quote Originally Posted by accmaster View Post

    Can you please check in my post #1 to detect mouse click is a professional work or it can create any issues on long terms basis?
    If only I could I would be glad to but I am not a professional developer, I am just an amateur who know some stuff (I master google search ). I happen to have played with some hook some time ago just for the fun.
    The important thing I remember about Hooks is that you must release them as soon as possible after the use because they can create instabilities in the system.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    277

    Re: [RESOLVED] Detect keyboard keydown event anywhere on screen, inside or outside pr

    @Delaney

    Thanks for your reply dear. My both mouse and keyboard detection issues are now resolved.

    Thank again...



    Ladak

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