none
Detecting Key Press While Window is Not on the Top? RRS feed

  • Question

  •     Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            If (e.KeyCode = 107) Then
                MsgBox("Pressed")
            End If
        End Sub

    I have these codes to detect a key press, however, if other forms are on top, this will not work. Is it possible to make it also work while not on top or minimized? 

    Friday, April 12, 2019 6:09 AM

All replies

  • Hi,

    try to rewrite keyboard events

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            If keyData = Keys.Add Then
                MessageBox.Show("Pressed")
            End If
            Return True
        End Function
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 12, 2019 6:29 AM
  • I have these codes to detect a key press, however, if other forms are on top, this will not work. Is it possible to make it also work while not on top or minimized? 

     Yes but, not with any standard .Net methods.  Doing that would require you to use one or the other of the win32 api functions I describe below.

     If you are using this to detect 'hotkeys' like Ctrl+K or just K for your application,  then perhaps just registering the hotkeys for your application by using the RegisterHotKey function and UnregisterHotKey function .  You can find many examples around the internet for this.  I have a basic commented example in my first post of this link ->  Hotkeys/Form focused or not

     

     Another more complex way of doing this would be to use the SetWindowsHookExW function to create a Low Level Keyboard Hook to monitor any key activity at any time.  However,  posting code examples for a keyboard hook could be considered as helping with malicious code. Not all people would have good intentions for what a keyboard hook can do.

     One other more complex option would be to use the RegisterRawInputDevices function to register your application to receive all the KEYBOARD type devices raw message data.  This code could also be considered to be malicious code as it can be used to do the same thing as a keyboard hook,  just in a little different way.

     Although, I am not posting examples for the second two options, that does not mean examples can not be found on the internet.  8)


    If you say it can`t be done then i`ll try it

    Friday, April 12, 2019 9:31 PM
  • That's very helpful! Thanks!

    Tuesday, April 16, 2019 6:13 AM
  • Hi,

    If your issue is solved, please Mark as answer,so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 16, 2019 6:17 AM