News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Get Keyboard scan code from a console app

Started by pcMike, February 12, 2013, 12:42:30 PM

Previous topic - Next topic

dedndave

it's essentially the same code, just written in a different style

pcMike

Hi Dave,

I found a little bug in the AnyKey procedure. If the user presses Ctrl-C, it will shut down my application.
Is there any way to prevent this from happening? Otherwise I may end up needing to create a scan table
and stick with GetKey. :-(

Mike

dedndave

i have noticed some "peculiarities" in the routine, also
for example, when pressing keys like shift, control, alt, the routine exits with a keystroke
it is somewhat debatable if that is desirable - lol
and - another debate for caps lock, scroll lock, num lock, etc
these issues can be handled by simply adding filters to the code, and looping back
let me know if you want an example of how to do this

as for the specific problem of Ctrl-C - it's not really a bug - it's considered normal   :biggrin:
you could use SetConsoleCtrlHandler during program initialization to alter this behaviour
however, i don't think you can avoid the Ctrl-Break combination

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686016%28v=vs.85%29.aspx

    INVOKE  SetConsoleCtrlHandler,NULL,TRUE

dedndave

ok - i was wrong..... again   :biggrin:

you can capture Ctrl-Break by writing a handler routine

in the attached test program, i wrote a simple handler that intercepts both Ctrl-C and Ctrl-Break
i also added filters to AnyKey so that it does not exit for shift, control, or alt keys

now, the problem is that no keystroke is generated for Ctrl-C
i may be able to insert that stroke into the stream manually
let me try it on the next go

note: there are numerous Ctrl-? strokes that do not place anything into the stream (Ctrl-S, for example)

pcMike

Thanks Dave that did the trick for Ctrl-C. I'm not too concerned about Ctrl-Break as that seems to kill most Console programs, so I can live with that. I did have to add filters to ignore the plain ALT, CTL and SHIFT key results... but since it's named AnyKey it lives up to it's name. :)

Mike

dedndave

yah - my initial use for this routine was simply "wait for any key to be pressed"
i decided to add the extra functionality because the info was there   :P

pcMike

Your new test version works very well so far.  I don't need to test for Ctrl-C in my app so I may use this version instead. :-)

Mike

dedndave

ok - well, if you need to, you can use WriteConsoleInput to insert INPUT_RECORD structures into the stream
for each keypress, you would want a record for key down and a record for key up