The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: digelo on June 07, 2010, 02:40:49 PM

Title: Console app Inkey
Post by: digelo on June 07, 2010, 02:40:49 PM
Hi
i found this Proc at older posts about reading keypress in console :

InKyb   PROC

;Poll keyboard

;  Returns: if no key: eax = 0
;                      ZF = set (zero)
;
;              if key: eax = virtual scan code
;                      ZF = clear (not zero)
;
InKyb0: push    eax
        INVOKE  PeekConsoleInput,
                hStdInp,
                ADDR InpEvntRec,
                1,
                esp
        pop     eax
        or      eax,eax
        jz      InKyb1             ;ZF set if no key

        INVOKE  ReadConsoleInput,
                hStdInp,
                ADDR InpEvntRec,
                1,
                ADDR EvntCnt
        dec word ptr InpEvntRec.EventType
        jnz     InKyb0             ;non-keyboard event type - check again

        dec dword ptr InpEvntRec.KeyEvent.bKeyDown
        jnz     InKyb0             ;keypress only

        xor     eax,eax
        inc     eax                ;ZF cleared if key
        movzx   eax,word ptr InpEvntRec.KeyEvent.wVirtualScanCode

InKyb1: ret

InKyb   ENDP


How can i convert its result to a Char !?  :red
Title: Re: Console app Inkey
Post by: qWord on June 07, 2010, 03:06:44 PM
Quote from: msdn readerInpEvntRec.KeyEvent.AsciiChar
KEY_EVENT_RECORD (http://msdn.microsoft.com/en-us/library/ms684166(VS.85).aspx)
Title: Re: Console app Inkey
Post by: oex on June 07, 2010, 03:25:28 PM
Awesome.... am I right in thinking with this I can add click events to my console apps?

http://msdn.microsoft.com/en-us/library/ms683499(v=VS.85).aspx
MOUSE_EVENT_RECORD
Title: Re: Console app Inkey
Post by: qWord on June 07, 2010, 03:29:58 PM
Quote from: oex on June 07, 2010, 03:25:28 PM
am I right in thinking with this I can add click events to my console apps?
yes we can :bg
Title: Re: Console app Inkey
Post by: oex on June 07, 2010, 03:32:37 PM
:bg
Title: Re: Console app Inkey
Post by: frktons on June 07, 2010, 03:59:34 PM
Quote from: digelo on June 07, 2010, 02:40:49 PM
Hi
i found this Proc at older posts about reading keypress in console :

InKyb   PROC

;Poll keyboard

;  Returns: if no key: eax = 0
;                      ZF = set (zero)
;
;              if key: eax = virtual scan code
;                      ZF = clear (not zero)
;
InKyb0: push    eax
        INVOKE  PeekConsoleInput,
                hStdInp,
                ADDR InpEvntRec,
                1,
                esp
        pop     eax
        or      eax,eax
        jz      InKyb1             ;ZF set if no key

        INVOKE  ReadConsoleInput,
                hStdInp,
                ADDR InpEvntRec,
                1,
                ADDR EvntCnt
        dec word ptr InpEvntRec.EventType
        jnz     InKyb0             ;non-keyboard event type - check again

        dec dword ptr InpEvntRec.KeyEvent.bKeyDown
        jnz     InKyb0             ;keypress only

        xor     eax,eax
        inc     eax                ;ZF cleared if key
        movzx   eax,word ptr InpEvntRec.KeyEvent.wVirtualScanCode

InKyb1: ret

InKyb   ENDP


How can i convert its result to a Char !?  :red

Ah Ah, this is what I'm trying to do in C, before switching to MASM and translate
the code into ASM, but actually it looks like I can do reverse.  :lol
Title: Re: Console app Inkey
Post by: dedndave on June 07, 2010, 06:48:25 PM
it might be easier to use crt getch (corrected)   :P
and, it probably does a nicer job of it
i have tried messing with this to some degree
i always wind up with mouse-related bugs (some console bugs can't even be avoided by using getkey)
whatever you do, you may want to test the result with mouse select all/copy/paste activity
Title: Re: Console app Inkey
Post by: GregL on June 07, 2010, 09:20:43 PM
Personally I prefer CRT _getch().  :P  But anyway, a group of us here created a _getch() replacement that used only the Windows API. Like Dave said, it was unreliable under certain situations. That is why the MASM32 library uses the CRT functions for the procedure wait_key, macro inkey etc.

Title: Re: Console app Inkey
Post by: oex on June 07, 2010, 10:16:12 PM
My requirements are limited so hopefully I wont come across the same issues, the click functionality is what I'm after to some degree but thanks for the heads up on the selection stuff I had considered that this might conflict

Was it feature rich or buggy? :lol
Title: Re: Console app Inkey
Post by: dedndave on June 07, 2010, 10:21:19 PM
QuotePersonally I prefer CRT _getch()

lol Greg - i stand corrected - that's the one i meant   :bg

it is actually the console that is buggy, i think
and the API doesn't always give you enough hooks into it to create work-arounds
as far as i know, it has always been buggy - although, things may have gotten better with Win7

some of the problems that you may run into go something like this
the SetConsoleMode function does not work as expected
for example, i tried to disable the mouse functions altogether, but was not able to do so
in the console input message queue, there are some messages that are, shall we say "reserved" for the OS
be sure and filter out the ones you want to handle
but - be prepared (maybe) to write some code to handle some other messages - lol

one thing i was seeing was that the user could click on Select All, Copy, and/or Paste and the console loses it's line position
that is to say, positioning the cursor after such operations may act wiggy   :P
also, if the user decides to click Paste and stick some text into "your" display area - there is no way to know about it
the cursor is no longer where you think it is
and, there is really no way to disable it (eg. SetConsoleMode is no help)
these mouse functions, along with the keyboard, all put messages into the queue
at that point, i quit playing with it
there may be some magical way of interpreting these "OS reserved" messages and writing work-arounds
but, they are not documented, so it will be imperical discovery day if you do - lol

at that point, i accepted the fact that the console is not a good place to write "major" applications
any program that tries to maintain a dynamic console display may be susceptible to bugs
it is a good place for simple tool type programs, like the many that already exist (simple TTY serial output text, etc)
i think that is why the console has the bugs - MS does not see a need to fix them
they regard it as a playground for simple tools - nothing more
Title: Re: Console app Inkey
Post by: oex on June 08, 2010, 12:16:44 AM
:lol All I want to do is have a clickable menu but that is seeming rather difficult.... Maybe clickable lines that hold file paths etc....

Right now I'm stuck on font sizes.... WTF are logical units.... My font is 120x78 Logical Units....

This is so difficult I'm tempted to spend the rest of my life writing my ASM editor in console also to give QED a run for it's money :lol

http://en.wikipedia.org/wiki/Quantum_electrodynamics
Title: Re: Console app Inkey
Post by: dedndave on June 08, 2010, 11:58:12 AM
i would recommend you go with a GUI program, rather than fighting with the console   :P

ps - i just realized the code in the first post is mine - lol
no wonder he's having trouble   :P
Title: Re: Console app Inkey
Post by: digelo on June 08, 2010, 12:16:36 PM
Quote from: dedndave on June 08, 2010, 11:58:12 AM

ps - i just realized the code in the first post is mine - lol

haha
Yea i found it in older posts.
and Thank you so much about it cuz this code was exactly what i wanted :toothy
Title: Re: Console app Inkey
Post by: dedndave on June 08, 2010, 01:14:55 PM
well - that was just an attempt
as i said, you would be better off to use the getch function from the crt library
        call    crt__getch

better yet - it is already written for you
have a look at this file...
\masm32\m32lib\wait_key.asm
that function flushes the input buffer first, though
that may not be desirable
Title: Re: Console app Inkey
Post by: oex on June 08, 2010, 01:48:33 PM
You know, no offense Dave but could you have made that code any longer :lol.... What's with one arg on every line? I do like your usage of esp though

I think I have found the selection issue you were talking about and the toggle for it but not sure.... I'm going to have to have a good play with this this evening and see what I can come up with.... It seems like it will do what I want, I think it's just going to be a pain to implement
Title: Re: Console app Inkey
Post by: oex on June 08, 2010, 07:10:44 PM
Dave, can you just not toggle select off with SetConsoleMode?
Title: Re: Console app Inkey
Post by: dedndave on June 08, 2010, 11:06:01 PM
try it out
you can also use GetConsoleMode to see what it is set to
at one time, i wrote a little program that went through 0 to 256
it set the mode, then read it back and displayed only the valid mode settings
lost that little program on a crashed hard drive, though - it wasn't hard to write
Title: Re: Console app Inkey
Post by: oex on June 09, 2010, 12:52:13 AM
:bg, yep I seem to be using it successfully that was for your benefit :lol.... Should have know you were ten steps ahead of me