I am trying to write code to check whether or not Capslock is on using VB.net. I have found some code online but there seems to be a problem. Here is what I have tried so far.
If My.Computer.Keyboard.CapsLock = True Then
Dim intmessage As Long
intmessage = MsgBox("Would you like to exit the loop and close the application?", vbYesNo)
If intmessage = vbYes Then Application.Exit()
End If
If Control.IsKeyLocked(Keys.CapsLock) = True Then
Dim intmessage As Long
intmessage = MsgBox("Would you like to exit the loop and close the application?", vbYesNo)
If intmessage = vbYes Then Application.Exit()
End If
I have a long series of repetitive tasks that I want to run on my computer. But I also want the ability to pause these tasks with the press of a button. The idea is that I will loop through these tasks, periodically checking the Capslock, which I will use if I desire to interrupt the tasks. As the loop continues, if I turn on Capslock, I will have the option of pausing or exiting the loop. For whatever reason, these two attempts are only catching the initial keystate of Capslock when the application is launched. If Capslock is on when I launch the application and begin the loop, these always return True. If Capslock is off when I launch the application and begin the loop, these always return False. Its like I need to refresh the keyboard control or something. What I need is to begin with Capslock off, but to properly be updated whenever I turn on Capslock.
Thanks.
On the Form designer add a timer, under Components. Set the interval to 1000 and enable to true.
Then add this code.
This code forces CapsLock to be cycled off / on before asking question again. Notice the use of MessageBox and DialogResult.
EDIT:
Another approach using WndProc
Then in your code call this periodically,
BUT - If you have the UI locked up this won't work either.