flat assembler
Message board for the users of flat assembler.

Index > Windows > [beginner] How to control program using keyboard?

Author
Thread Post new topic Reply to topic
mkng



Joined: 13 May 2012
Posts: 5
mkng
Hi. I'm begginer trying to learn assembler. I'm trying to modify the transparent window example to make it able to control everything by keyboard. I was trying to use something like this:

Code:
in    al,60h
cmp   al,0x50      ; scan code for down arrow
je   przyciskMniej    

or
Code:
xor   ax,ax                         ; Get a key
int   16h                           ; Call interrupt service
cmp   al,13                         ; Enter?
je   przyciskMniej    

but my program just crashes and I have no idea what's going on. These codes were taken from this forum. Can anobody help me please? Smile


Last edited by mkng on 13 May 2012, 13:45; edited 1 time in total
Post 13 May 2012, 10:06
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17452
Location: In your JS exploiting you and your system
revolution
In Windows you can't use direct I/O because it is a protected OS.

You have to call some of the API functions like ReadFile.
Post 13 May 2012, 10:56
View user's profile Send private message Visit poster's website Reply with quote
mkng



Joined: 13 May 2012
Posts: 5
mkng
revolution wrote:
In Windows you can't use direct I/O because it is a protected OS.

You have to call some of the API functions like ReadFile.

Can you be more precise? How ReadFile could help me with keyboard?
Post 13 May 2012, 10:58
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17452
Location: In your JS exploiting you and your system
revolution
Get the STDIN handle and then use ReadFile to get the characters.

There are already example of code for this on this forum.
Post 13 May 2012, 11:00
View user's profile Send private message Visit poster's website Reply with quote
mkng



Joined: 13 May 2012
Posts: 5
mkng
Don't think I'm lazy or stupid, but... I can't find any example that could be helpful. Everything I've found is about console applications or dialog boxes. Any clue?
Post 13 May 2012, 11:34
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1412
Location: Toronto, Canada
AsmGuru62
Before you start with a keyboard -- what kind of application you will
be controlling? There are two type of applications for Windows:

- Console (you just see a black box, like DOS, but it is NOT DOS!)
- GUI (you have one or more windows)

So, what kind of application it will be?

P.S. The keyboard input is programmed DIFFERENTLY for these two app types.
Post 13 May 2012, 11:45
View user's profile Send private message Send e-mail Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
Hi,

for the specific GUI example simply add a WM_KEYDOWN event
Code:
        cmp [wmsg], WM_KEYDOWN
        je wmkeydown  
    

Code:
wmkeydown:
        ; get WM_KEYDOWN message
        push [wparam]
        pop eax
        ; examine virtual-key code
        .if (al = VK_LEFT)
            jmp lineleft
        .elseif (al = VK_RIGHT)
            jmp lineright
        .endif
        jmp     defwndproc  

      ; see also http://msdn.microsoft.com/en-us/library/windows/desktop/hh298359(28v=vs.85).aspx

    


Use arrow keys to scroll, if that is what you want.
Post 13 May 2012, 11:50
View user's profile Send private message Reply with quote
mkng



Joined: 13 May 2012
Posts: 5
mkng
@AsmGuru62: GUI.

@Picnic: great, thank you! But...
I understand that You gave me a pseudocode, right? Now how should I write the "if" section in assembler? I've tried this:
Code:
cmp     AL, WM_KEYDOWN
je      lineleft    

but I get "value out of range" error.


Last edited by mkng on 13 May 2012, 13:45; edited 1 time in total
Post 13 May 2012, 12:37
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
WM_KEYDOWN is a 16-bit value while al an 8-bit register.

It's full working code.
Replace the line (sorry my bad),
Code:
include 'win32w.inc'   
    

with
Code:
include 'win32wx.inc'   
    


may be better to start with fasm documentation, learn about windows programming headers here, then read more about windows messages in msdn, before you start writing code.
Post 13 May 2012, 12:45
View user's profile Send private message Reply with quote
mkng



Joined: 13 May 2012
Posts: 5
mkng
Wow, that's pretty cool! Thank you! Smile

Looking at the code I wonder why there's jump to defwndproc (just after .endif)? It seems that this jump will never be executed, so there's no need for it to be there.

By the way - just to know - this ".if" clauses are equal to what asm code? I mean - if I wanted to write equal if-clause in pure assembler, how should it look?
Post 13 May 2012, 12:59
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
Actually the jump will executed every time there is a WM_KEYDOWN event which is not a VK_LEFT or VK_RIGHT.

Code:
wmkeydown:
        ; get WM_KEYDOWN message 
        push [wparam]
        pop eax

        ; examine virtual-key code 
        cmp al, VK_LEFT
        je lineleft
        cmp al, VK_RIGHT
        je lineright
        ;
        ;       
        jmp     defwndproc  
    
Post 13 May 2012, 14:18
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1412
Location: Toronto, Canada
AsmGuru62
@Picnic: why not 'mov eax, [wparam]'?
Post 13 May 2012, 14:28
View user's profile Send private message Send e-mail Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
AsmGuru62 wrote:
@Picnic: why not 'mov eax, [wparam]'?


Maybe a personal style of coding. Cool
Post 13 May 2012, 17:38
View user's profile Send private message Send e-mail 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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.