Imports System.Windows.Forms Imports System.Runtime.InteropServices Sub Main For i As Integer = 1 To 100 Threading.Thread.Sleep(20) ThisApplication.UserInterfaceManager.DoEvents Dim keyDown = Keyboard.IsKeyDown(Keys.ShiftKey) If keyDown Then Logger.Debug("shift key down = {0}. Exiting loop.", keyDown) Exit For ' use Exit Do for Do loops, or Exit While for While loops End If Next End Sub ''' ''' See https://stackoverflow.com/questions/1100285/how-to-detect-the-currently-pressed-key ''' Public Class Keyboard Private Enum KeyStates None = 0 Down = 1 Toggled = 2 End Enum Private Shared Function GetKeyState(ByVal keyCode As Integer) As Short End Function Private Shared Function GetKeyState(ByVal key As Keys) As KeyStates Dim state As KeyStates = KeyStates.None Dim retVal As Short = GetKeyState(CInt(key)) If (retVal And &H8000) = &H8000 Then state = state Or KeyStates.Down If (retVal And 1) = 1 Then state = state Or KeyStates.Toggled Return state End Function Public Shared Function IsKeyDown(ByVal key As Keys) As Boolean Return KeyStates.Down = (GetKeyState(key) And KeyStates.Down) End Function Public Shared Function IsKeyToggled(ByVal key As Keys) As Boolean Return KeyStates.Toggled = (GetKeyState(key) And KeyStates.Toggled) End Function End Class