Hi
At first I thought, 'this should be quite simple' , but after scouring the documentation etc. I can see why you asked.
Despite providing a SetKeys() function to stuff the keyboard buffer and feed keys to the window with focus, the Cypress VBS does not have a corresponding GetKeys() function.
Historically, I would have used Int 9 in assembler to interrogate the keyboard buffer, when I used to program in Windows years ago.
Not much use in VBS though.
The main problem in any windowing system, is that mouse and keyboard input is directed and read in terms of which window has focus.
Usually this will be by way of this sort of psuedo code
winptr = GetWindowWithFocus()
KeyPressed = winptr->keyboardEvent.GetKey()
You want a system wide remit, to see what is in the keyboard buffer and act on an Escape sequence.
So far as I can ascertain, the way to read the keystrokes at a system level in Windows using VB, is to access the functions in the user32.dll, namely
GetAsyncKeyState()
The below code should test the keyboard buffer for an Escape key press, by calling EscapeKeyPressed() from within your code.
'''''Determining if Escape has been pressed (vbKeyEscape)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Function EscapeKeyPressed() As Boolean
Dim bResult As Boolean
'Check First Key (need to call twice, the first call clears
'the keyboard buffer then second checks it's still pressed)
If VarType(vKey1) = vbString Then
bResult = CBool(GetAsyncKeyState(Asc(vbKeyEscape)))
bResult = CBool(GetAsyncKeyState(Asc(vbKeyEscape)))
Else
bResult = CBool(GetAsyncKeyState(vbKeyEscape))
bResult = CBool(GetAsyncKeyState(vbKeyEscape))
End If
EscapeKeyPressed = bResult
Exit Function
End Function
One possible place to put this test is inside your
While IsMoving() loops
You have a lot of these, and should in any case have something like sleep(100) inside them to allow processor time back to the system whilst waiting on the completion of a move.
Something like this might be a starting point
Dim abort As Boolean
abort = false
.
.
.
While IsMoving()
abort = EscapeKeyPressed()
If abort = true Then
'''Initiate whatever needs doing here
End If
sleep(100)
Wend
Alternately, you could simply periodically call EscapeKeyPressed() thoughout the code.
I am no expert on VB or VBS, but I have seen other code where the functions from system .dll s are declared and then the function called, so it should work.
Hopefully someone else can assist with the exact coding required to get it to work in VBS if you have problems.
regards
Melee
PS
I seem to remember reading that the Escape key was not actually a good key to use for anything in macros, because it would likely conflict with the hard coded keybindings in Mach.
You might want to use the Space Bar, commonly used as a pause signal in various apps, so vbKeySpace or Chr(0x20) maybe.