即使用户按下另一个键,int 16h/ah = 1也会反复给出相同的按键 [英] int 16h/ah=1 repeatedly gives the same key press even after user presses another key

查看:151
本文介绍了即使用户按下另一个键,int 16h/ah = 1也会反复给出相同的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用汇编语言编写游戏代码,其中恐龙的运动取决于用户按下的键.我以某种方式实现了我的代码,即如果用户按下空格键,则程序应终止,并且恐龙在按下"a"时应向正确的方向移动.但是我面临的一个问题是,在第一次按下任何键之后,程序将不查找被按下的其他任何键,这表明它一次又一次地按下了第一个被按下的键.如何解决?

I am writing code of a game in assembly language where the movement of the dinosaur depends on the key pressed from user. I have implemented my code in a way that if the user presses space bar the program should terminate and the dinosaur should move in a right direction on pressing "a". But I am facing a problem that after pressing any key for the first time the program doesn't look for any other key which is pressed after that which shows it is taking the first pressed key again and again. How to fix it?

我正在使用mov ah,01h和int 16h的功能,该功能返回al寄存器中按下的键的ascii.之后,我将其与所需的ascii键进行了比较,但是这16h仅在第一次按下任何键时效果良好.

I am using the function of mov ah, 01h and int 16h which returns ascii of pressed key in al register. after this I compare this with required ascii keys but this 16h is doing well only when any key is pressed for the first time.

label2: 
        mov ah, 1h
    int 16h
    mov keyboardkey, al        ;keyboardkey is variable for storing ascii of key pressed

    .IF( keyboardkey == 32 )
        ret
    .ENDIF

    .IF( keyboardkey == 97 )
            mov bx, startingXaxis
            add bx, 10
            mov startingXaxis, bx
            call drawdinosaour
    .ENDIF

    .IF (keyboardkey == 98 )
        mov bx, startingXaxis
        sub bx, 10
        mov startingXaxis, bx
        call drawdinosaur
    .ENDIF  

        call delay              ;this function passes the program for 1/4 second
        jmp label2

我期望每当我按下空格键时,该程序都会终止,但它只会选择第一次按下的键,此后它将继续根据该第一个键进行操作,并且不会查找此后按下的任何键

I was expecting that whenever i press spacebar the program will terminate but it only picks the key pressed for the first time and after that it keeps on acting according to that first key and doesn't look for any key that is pressed afterwards

推荐答案

int 0x16, ah =0x01不会从缓冲区中删除键,并且(如果未按下任何键)不会等到用户按下一个键.

The int 0x16, ah =0x01 doesn't remove the key from the buffer, and (if no key was pressed) doesn't wait until the user presses a key.

int 0x16, ah =0x00确实从缓冲区中删除了键,并且(如果没有按任何键)确实要等到用户按下一个键为止.

The int 0x16, ah =0x00 does remove the key from the buffer, and (if no key was pressed) does wait until the user presses a key.

如果要从缓冲区中删除键,但又不想等到按键被按下,请执行以下操作:那么您必须同时使用这两种功能:

If you want to remove the key from the buffer but don't want to wait until a key is pressed; then you have to use both functions, sort of like:

    mov ah,0x01
    int 0x16
    jz .doneKeypress
    mov ah,0x00
    int 0x16
    call handleKeypress
.doneKeypress:

关于您正在做的事情(有延迟,用户在执行延迟时可以按多个键);您可能想要更多类似的东西:

For what you're doing (with a delay, where the user could press multiple keys while you're doing the delay); you probably want something more like:

mainLoop:

;Handle all keypresses

.nextKey:
    mov ah,0x01
    int 0x16
    jz .doneKeypresses
    mov ah,0x00
    int 0x16
    call handleKeypress
    jmp .nextKey
.doneKeypresses:

;Update the screen

     call updateScreen

;Do the delay

    call delay

;Keep doing the loop while the game is running (and stop when the game stops)

    cmp byte [isRunning],0
    je mainLoop
    ret

这篇关于即使用户按下另一个键,int 16h/ah = 1也会反复给出相同的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆