News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Console app Inkey

Started by digelo, June 07, 2010, 02:40:49 PM

Previous topic - Next topic

digelo

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

qWord

Quote from: msdn readerInpEvntRec.KeyEvent.AsciiChar
KEY_EVENT_RECORD
FPU in a trice: SmplMath
It's that simple!

oex

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
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

qWord

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
FPU in a trice: SmplMath
It's that simple!

oex

We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

frktons

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
Mind is like a parachute. You know what to do in order to use it :-)

dedndave

#6
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

GregL

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.


oex

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
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

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

oex

: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
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

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

digelo

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

dedndave

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

oex

#14
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
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv