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.