0

I'm currently trying to modify an existing application I've already completed that's basically a glorified mp3 player in Visual Studio 2019. I can't find any ways to get the application to read keystrokes while the application is focused/unfocused. I tried using the solution from this older post and multiple others across the internet I could find but none seem work for me.

I added the KeyboardHook class to the project and declare the following at the beginning of the form1 public class that hosts the application functionality

Dim Lock As Boolean 'controls whether the application will accept keystrokes
Private WithEvents kbHook As New KeyboardHook

I then set the Lock boolean as true in Form1_Load.

This is the code I'm attempting to use to call an action when the " \ " key is pressed. The intended functionality is to change the value of the Lock boolean to true/false if it's false/true then play a sound effect for user feedback.

    Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown
        If Key.ToString = "\" Then
            If Lock = False Then
                Lock = True
            Else
                Lock = False
            End If
            My.Computer.Audio.Play("C:\Users\[MyUsernameHere]\source\repos\MP3 Player\MP3 Player\MP3 Player SFX\Unknown 11.wav")
        End If
    End Sub

The problem I'm having is that when I press the " \ " key, nothing happens. The rest of the application works as intended and has been functioning since I made it about a year ago, but it's this new functionality that I can't seem to get to work.

TLDR: I can't get my vb.net application to read keystrokes in the application despite applying/following multiple online code snippets/tutorials.

Priam
  • 1
  • Key.ToString does not do what you think it does. You'll need to debug it. – Hans Passant Aug 10 '20 at 00:42
  • I'm a bit new to stack overflow so I'm not sure how to reply to your comment Hans, but would you be able to provide an example of what to use in that if statement where Key.ToString is present to get the behavior I'm looking for? I'm not sure how to debug the application (not very experienced with coding) and am having trouble finding what .ToString does on the internet, making it hard for me to address the issue alone. Thanks for the response! – Priam Aug 10 '20 at 03:46
  • StackOverflow is to help you with specific coding questions but isn't here as a more general programming tutorial. In Visual Studio set a breakpoint on the line 'If Key.ToString = "\" Then' then run the code. It will stop when that line is reached and allow you to test for the value of Key. You'll see you need to test on its value, not covert it to a string. If you're unsure how to debug, then I'd suggest spending a little time learning that first and it will save you hours of coding time in future. Good luck. – Jon Roberts Aug 10 '20 at 09:06
  • I was able to figure out the breakpoint and debug thanks to your comment and address the issue. I feel kind of silly now that I see how easy it was to do but that's ok. By using the two, I was able to see that the backslash key is actually recognized as "Oem5" and revised the line of code to `If Key.ToString = "Oem5" Then`. This works as intended with the application focused/unfocused. I was mislead by the older referenced post above since one of the bottom comments simply put `if Key.ToString() = "a" then`. Thanks for the feedback, this really helped me out a lot. – Priam Aug 10 '20 at 13:38

0 Answers0