getAnyKey?

I have searched the forum and cannot find exactly what I need, but it must be there.

I want to be able to: stop all execution and have a simple loop that does nothing until any key is pressed and then the code will continue from where it was stopped.

while getAnyKey = “”
getKey from keyboard
wend
continue code

I’ve studied some timer code but I would like for it to be simple do nothing but wait for any key

uhm… if this function wouldn’t be so evil I would say this is the perfect tight loop for DoEvents :wink:

ConsoleApplication:

[code]// App.Run event
Function Run(args() As String) As Integer
Dim s As String

Do
s = Input()
If s <> “” Then
Exit
End
App.DoEvents()
Loop

Print "The following letter was pressed: " + s
End[/code]

In a desktop application you use the window’s (or a control’s) KeyDown event:

// Window1.KeyDown event Function KeyDown(Key As String) As Boolean System.DebugLog "The following letter was pressed: " + key End Function

Have you tried Keyboard.AsyncKeydown? I would normally discourage such a loop because it blocks the entire application and runs contrary to good event-driven methodology, but sometimes you gotta do what you gotta do.

I rather use AsyncKeydown in a timer than in a loop.

Yeah, but if you absolutely have to freeze the entire app until you get a keystroke, then a loop is the answer. Of course, that is usually not the best solution. It is possible the OP has an extreme scenario. Most of the time, that isn’t the case, but we don’t know yet.

I think I need to explain a bit more of what is going on when I need this code. The routine is running parsing text, therefore it is in a loop at the time looking at input it gets from a buffer. When it sees a certain character, I want to stop it and do some display work and have it wait for any keyboard input before continuing the process. Sorry I didn’t give this info before.

Why not thread the parsing and display the results as they happen?

Use a Modal window. It pauses the flow of your current code until the user dismisses it.

Thanks all. Would eventually like a trouble free, stop all, wait on keyboard, but for now decided to use a modal window, does pretty much what I need.

That’s an outdated programming idiom that isn’t really compatible with modern gui operating systems. It will get your process flagged as hung by the OS and possibly terminated.

Best question: why?
In a DOS app from 1980, perhaps ‘press a key between A to K’ as a menu.

In a GUI app, present a menu or a set of buttons.

The modal dialog (for now decided to use a modal window, does pretty much what I need.) is an alternative.

But what exactly are you trying to present to the user?

Why? Just for the heck of it and it keeps my 73 year old brain working.

So I get it. I am going with a custom modal window with some buttons. Works great. And Jeff almost figured out what I am doing. Writing an app which I ported from 8080 assembly to 68000 assembler back in the 80’s. Did it again in C at some stage and now xojo. The app is an archaic language that processes text. To debug a routine written in the language a command can be put in the routine which causes it to execute one command at a time as user presses a key on original versions. But buttons will work fine.

[quote=238268:@Dudley Henderson]Why? Just for the heck of it and it keeps my 73 year old brain working.

So I get it. I am going with a custom modal window with some buttons. Works great. And Jeff almost figured out what I am doing. Writing an app which I ported from 8080 assembly to 68000 assembler back in the 80’s. Did it again in C at some stage and now xojo. The app is an archaic language that processes text. To debug a routine written in the language a command can be put in the routine which causes it to execute one command at a time as user presses a key on original versions. But buttons will work fine.[/quote]

Just for the sake of possibilities, the modal dialog window (lets call it PressAKey) can be 0x0 pixels on Mac. Put this in its Keydown event

me.close

When you want the program to halt execution, go

pressaKey.ShowModal

On Windows, instead, do this to place the dialog off screen (the zero width window shows as a 200x35 plain box) :

pressAKey.Left = -200 pressaKey.ShowModal

If you need to know the value of the key, add a string property to App or to a module, and set it with it.

You really want to display “Press any key to continue”, to avoid the user to think the app is hung and that he kills it (force quit in Mac).

Why? Because your old assembly code had exclusive control over the machine. It was the only thing running, so it could do whatever it wanted. Also, your users were trained to interact with their computer in a certain way. All that has changed. Your GUI app runs in a shared environment. It doesn’t have exclusive access to anything. And it is constantly talking to the OS in order to maintain the graphical interface where multiple independent processes are presented to the user as a unified screen display where they appear to be overlapping windows and the OS plays referee to make sure they all play together nicely. This is where tight loops that “freeze” the application waiting for user input are dangerous.

You could implement your code in a Console Application, which would run in an artificial environment that is much closer to what you were used to back in the day. Then a “pause for input” type approach would make sense. But for a GUI/windowed app, the equivalent function is a modal window. Or a complete rethink of the approach to use the screen and keyboard in a different way.

Re-reading your first post, and depending on how you want to display the information, MsgBox may be sufficient for your needs. Re-reading your last post, my explanation probably wasn’t necessary. I read “why?” as asking for info, instead of responding to Jeff’s question. Sorry for the noise.

Thanks all, a lot of good information. Using modal window with keyboard input. So the user can click on button to single step, or any key to single step and a continue button to leave single step and finish running the routine.

Going back to ‘press a key’ then, what you might consider is this:

Your process is steps long.
You maintain a counter to know where you are in the process.

At startup, your counter is at position 1
When a key is pressed, you perform step <n+1> and increment the counter.
If the ‘do it all’ button is pressed, you perform all the following steps.

The process is handled by a method
It may as well do the incrementing too.
And it can set

[code]Private Function DoAStep() as boolean
static counter as integer //static means it retains the value between calls
//perform step
//counter = counter+1

return (counter > laststep) //signal process complete

end sub[/code]

The Start button makes a call to DoAStep and disables itself (so you dont re-enter)
The Step button makes a call to DoAStep
Add a KeyDown event to the window… if it is called, then call DoAStep

If the ‘DoItAll’ button is pressed, then repeatedly call DoAStep

while not DoAStep() wend

I’ve tried with every keyboard I’ve ever had and I still can’t find the Any Key.