Showing all pressed keys?


Could anyone post a small sample which can the following:

It should listen to the keystrokes and then it should visualize all pressed keys!

Thanks a lot

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this function...

Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading

Module Keyboard
    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 LLKHF_INJECTED As Integer = &H10
    Private Const LLKHF_ALTDOWN As Integer = &H20
    Private Const LLKHF_UP As Integer = &H80

    ' Virtual Keys
    Public Const VK_TAB = &H9
    Public Const VK_CONTROL = &H11
    Public Const VK_ESCAPE = &H1B
    Public Const VK_DELETE = &H2E

    Private Const WH_KEYBOARD_LL As Integer = 13&
    Public KeyboardHandle As Integer

    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
            Dim k As Keys = CType(lParam.vkCode, Keys)
            Debug.WriteLine("Pressed Key : " & k.ToString("G"))
        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
            Debug.WriteLine("Keyboard hooked")
            Debug.WriteLine("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

To start hook,

To Stop Hook

RuttensoftAuthor Commented:

Unfortunatly I seem to have described this a bit too bad...

What I want is:

after starting listening, it should give back all pressed keys. E.G. if I am pressing Ctrl, Alt and Delete, it should give me "Ctrl+Alt+Del" or something in one line...

after releaseing the Ctrl-Key, it then should only be "Alt+Del"


are u want to trap CTRL + ALT + DEL ???
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

RuttensoftAuthor Commented:
No, not really :-)

I just want to track several keys at once...
Fernando SotoRetiredCommented:
The keyboard key combination of "Ctrl+Alt+Del" is not interceptable because when those keys are pressed the keyboard immediately raises a hardware interrupt directly to the CPU.
Place this i your form and tested it....


   Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Const WM_KEYDOWN As Integer = &H100
        Const WM_SYSKEYDOWN As Integer = &H104

        If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
            Select Case (keyData)
                Case Keys.Right
           "Right Arrow Captured"
                Case Keys.Left
           "Left Arrow Captured"
                Case Keys.Down
            "Down Arrow Captured"
                Case Keys.Up
           "Up Arrow Captured"
                Case Keys.Tab
            "Tab Key Captured"
                Case (Keys.Control Or Keys.M)
           "<CTRL> + M Captured"
                Case (Keys.Alt Or Keys.Z)
           "<ALT> + Z Captured"
            End Select
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
Diego PazosCommented:
See if this is OK for you.

private void Form1_KeyDown(object sender, KeyEventArgs e) {
    string S="";
    if (e.KeyCode==Keys.ShiftKey || e.KeyCode==Keys.ControlKey || e.KeyCode==Keys.Alt) {
    if (e.Control) S+="CTRL + ";
    if (e.Alt) S += "ALT + ";
    if (e.Shift) S += "SHIFT + ";
    S += e.KeyCode;
Diego PazosCommented:
Sorry. I made a mistake. The keycode for ALT is called 'Menu'. Here goes the corrected code:

private void Form1_KeyDown(object sender, KeyEventArgs e) {
    string S="";
    if (e.KeyCode==Keys.ShiftKey || e.KeyCode==Keys.ControlKey || e.KeyCode==Keys.Menu) {
    if (e.Control) S+="CTRL + ";
    if (e.Alt) S += "ALT + ";
    if (e.Shift) S += "SHIFT + ";
    S += e.KeyCode;

Hope it works now.
Diego PazosCommented:
Oh, well... Before posting the correction I happened to change the MessageBox.Show to a Console.WriteLine.
Change it back in case you don't see any output.
Fernando SotoRetiredCommented:
I think the author wants it in VB .Net.
Diego PazosCommented:
There you go.

Private Sub Form1_KeyDown(object sender, KeyEventArgs e) Handles Form1.KeyDown
    Dim S As String= ""
    If e.KeyCode = Keys.ShiftKey Or e.KeyCode = Keys.ControlKey Or e.KeyCode = Keys.Menu Then
        Exit Sub
    If e.Control Then S +="CTRL + "
    If e.Alt Then S += "ALT + "
    If e.Shift Then S += "SHIFT + "
    S += e.KeyCode
End Sub

I translated it on a rush and had no chance to check it with the compiler so there may be slight errors, but that would be it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Diego PazosCommented:
Um... it just crossed my mind that you should change += by &= for VB.
Are u want to view the key strokes only for your form or global??
great uKER code works superbly.
RuttensoftAuthor Commented:
Thanks that was exactly what I wanted...

Only small question: is there a possibility to translate the Keycode automatically to the Keyname? (So not 86 but "v"?)


try this...

RuttensoftAuthor Commented:
moset welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.