flat assembler
Message board for the users of flat assembler.

flat assembler > Linux > Get pushed key

Author
Thread Post new topic Reply to topic
binary108



Joined: 03 Apr 2004
Posts: 7
Location: Russia
I have new problem Smile
How can my console program can know what key was pushed without display code of pushed button at display?
When use sys_read from std_in whith one-byte syze of bufer, code of pushed key is output on display.
Must i redirect std_out (if it is so, how to do it?) or there is another way?
Thanks
Post 19 Apr 2004, 23:53
View user's profile Send private message Reply with quote
binary108



Joined: 03 Apr 2004
Posts: 7
Location: Russia
Mda...
it seems something wrong in my post...

How can seample console program get scan-code (or another identifer) of seangle pushed button (or key - i don't know what's right) on keyboard?

Sorry for my bad english...
Post 20 Apr 2004, 07:26
View user's profile Send private message Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: usa


Last edited by coconut on 10 Mar 2005, 21:18; edited 1 time in total
Post 20 Apr 2004, 21:03
View user's profile Send private message Reply with quote
gorshing



Joined: 27 Jul 2003
Posts: 72
Location: Okla, US
I was unaware that you could use a hook for a system event in linux. Does anybody have any examples?

As for hiding characters being typed. I would look into using the ncurses library.

Otherwise, I would think that it would be different for each type of terminal ... I don't know though Confused

_________________
gorshing
Post 20 Apr 2004, 21:23
View user's profile Send private message Visit poster's website Reply with quote
0xkorkut



Joined: 10 Mar 2005
Posts: 4
Here is an example of getting single keys using linux kernel calls
i tried my best Laughing

Code:
;this proggy mainly shows the next char in 1 second after you typed
;i hate to see characters "being typed" while im not touching the keyboard
;so i added a buffer flush code
;i hope it would help someone

;equates are stolen from asmutils include files (os_linux.inc)
;some of them also can be found at www.lxhp.in-berlin.de/lhpioctl.html
;but they are well hidden so try searching the document for "termios"

;i came across this info after examining "snake" proggie in asmutils.
;here the keyword is "non-canonical". after googling for "canonical tty"
;i found this addr http://ou800doc.caldera.com/en/SDK_sysprog/_TTY_in_Canonical_Mode.html
;check out the link topright says "terminal device control" you should see
;a lot of good docs here

;also check out http://ou800doc.caldera.com/en/man/html.7/termio.7.html
;for better explanations for termios

;hold down the key to see what happens. ESC to quit.

;i apologize for my poor programming skills but i mainly tried to
;write more understandable code for newbies like me


format ELF executable

entry start

TCGETS    equ 0x5401
TCSETS    equ 0x5402
TCFLSH    equ 0x540b
          TCIOFLUSH equ 2          ;flush both input output
          TCOFLUSH  equ 1          ;flush output
          TCIFLUSH  equ 0          ;flush input
ICANON    equ 2
ECHO      equ 8
STDIN     equ 0
STDOUT    equ 1
SYS_NANOSLEEP equ 162
SYS_IOCTL equ 54
SYS_READ  equ 3
SYS_WRITE equ 4
SYS_EXIT  equ 1

section readable writeable
start:

call begingetkey         ;enter non-canonical mode
                         ;close local echo
mainloop:
mov ebx,timer            ;wait a little to fill keyboard buffer
mov ecx,timer2           ;simulate a time consuming process
mov eax,SYS_NANOSLEEP
int 0x80

mov ebx,STDIN
mov ecx,key
mov edx,1
mov eax,SYS_READ
int 0x80
push eax                 ;number of read bytes

mov ebx,STDIN            ;flush extra chars written to the
mov ecx,TCFLSH           ;buffer while we hold down the key
mov edx,TCIOFLUSH        ;in fact i dont know how it really effects
mov eax,SYS_IOCTL        ;but i prefer to flush both buffers
int 0x80                 ;it seems ok for this code

pop eax
test eax,eax             ;read anything?
jz mainloop

cmp byte [key],0x1b      ;ESC key quit
jz quitloop
inc byte [key]           ;just to see WE are echoing
                         ;show next char
mov ebx,STDOUT
mov ecx,key
mov edx,1
mov eax,SYS_WRITE
int 0x80

jmp mainloop

quitloop:

call endgetkey           ;restore previous state and quit

mov eax,ebx
mov eax,SYS_EXIT
int 0x80


begingetkey:
        mov ebx,STDIN             ;get old state here
        mov ecx,TCGETS
        mov edx,old_termios
        mov eax,SYS_IOCTL
        int 0x80

        mov ecx,19                ;and make a copy
        mov esi,old_termios
        mov edi,new_termios
        rep movsb

        and [new_termios.c_lflag],dword not(ICANON or ECHO)   ;turn off echo
        mov byte [new_termios.VMIN],0                    ;turn off canonical mode
        mov byte [new_termios.VTIME],0                   ;we dont want to wait for keystrokes

        mov ebx,STDIN
        mov ecx,TCSETS
        mov edx,new_termios
        mov eax,SYS_IOCTL
        int 0x80
        ret

endgetkey:                                                 ;clean up
        mov ebx,STDIN
        mov ecx,TCSETS
        mov edx,old_termios
        mov eax,SYS_IOCTL
        int 0x80
        ret

timer:               ;timespec structure
        dd 1         ;seconds
        dd 0         ;nanoseconds
timer2:
        dd 1
        dd 0

old_termios:         ;termios structure NOT termio
.c_iflag:
          rd 1
.c_oflag:
          rd 1
.c_cflag:
          rd 1
.c_lflag:
          rd 1
;.c_cc
       rb 4
       .VMIN:
               rb 1
       .VTIME:
               rb 1
       rb 13
new_termios:
.c_iflag:
          rd 1
.c_oflag:
          rd 1
.c_cflag:
          rd 1
.c_lflag:
          rd 1
;.c_cc
       .VINTR:
               rb 1
       .VQUIT:
               rb 1
       .VERASE:
               rb 1
       .VKILL:
               rb 1
       .VEOF:
               rb 1
       .VMIN:          ;in canonical mode VEOL
               rb 1
       .VTIME:         ;in canonical mode VEOL2
               rb 1
       .VSWTCH:
               rb 1
       .VSTRT:
               rb 1
       .VSTOP:
               rb 1
       .VSUSP:
               rb 1
       .VDSUSP:
               rb 1
       .VREPRINT:
               rb 1
       .VDISCARD:
               rb 1
       .VWERASE:
               rb 1
       .VLNEXT:
               rb 1
               rb 3       ;reserved
key:
           rb 1
    

_________________
.cC}< Eat your veggies >{Cc.
Post 10 Mar 2005, 20:42
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.