0

I've tried Flush Keyboard Buffer x86 assembly using int 16h 's solution, and it didn't work. I did everything I could to stop the buffer from happening and nothing seems to work.

I am currently making a game in assembly, and when I press for example "w" for too long, the character will continue moving for a little more then half a second after I stopped pressing "w".

this is my mainloop that detects when a key is pressed, but for some reason remember previous keys:

mainloop:
    mov ah, 1h
    int 16h
    jz after_press
    mov ah, 0h
    int 16h
    cmp ah, 11h                 ; 11h == scancode for W
    jne mlr1
    call link_up
    mlr1:
        cmp ah, 1Eh             ; 1Eh == scancode for A
        jne mlr2
        call link_left
    mlr2:
        cmp ah, 1Fh             ; 1Fh == scancode for S
        jne mlr3
        call link_down
    mlr3:
        cmp ah, 20h             ; 20h == scancode for D
        jne after_press
        call link_right
    after_press:
        mov ax, 5h
        mov bx, 0h
        int 33h
        cmp ax, 1h
        jne asrd
        call use_sword
    asrd:
    call octorok_ai
    mov bx, 1h ; delay time
    call delay
    jmp mainloop

If someone can please tell me how do I clear the buffer, I will be gratefull

Tomergngn
  • 64
  • 9
  • 1
    The reason your current code doesn't work is `INT 16 ah=1` doesn't check to see if a key is *pressed*. It's checking to see if there are keystrokes in the *buffer*. That's not helpful in your case. To see if any key is currently 'down', you'll probably need something more like [this](https://stackoverflow.com/a/40963633/2189500). – David Wohlferd Jan 26 '22 at 01:26
  • I'm sorry, but I don't quite understand. This code is in C++, whilst I'm working in assembly. Is that just the general idea behind the code, or is there a way to implement that code in assembly? – Tomergngn Jan 26 '22 at 11:20
  • Look up BIOS call `16h` function `03h` which sets the keyboard initial delay and auto repeat rate. With `bh` time to wait, `bl` time to repeat, that's all I have without digging. – Weather Vane Jan 26 '22 at 15:23
  • This isn't universal: please see this HTML version of Ralf Brown's page about [Int 16/AH=03h](http://www.ctyme.com/intr/rb-1757.htm). – Weather Vane Jan 26 '22 at 16:21
  • As you already understand, holding down the W key fills up a keyboard buffer. So as you say, the solution is to clear out that buffer. But *when* do you want to clear out the buffer? How do you know whether the next char in the buffer should be processed or discarded? If the user types WWWD, should you clear the buffer after the first W (discarding the D)? Or do you want to check when the W key is released and clear it then (that's what the link I gave you tries to do)? Maybe turn off auto repeat (that's what Weather vane's solution does)? – David Wohlferd Jan 26 '22 at 19:19
  • I think I want another way to get the keys. I want to check if a certain key is down right now, without a buffer or anything. because not doing that will make me move in ways I don't want, and has that weird delay between the first key and the others when I press on one key for long – Tomergngn Jan 30 '22 at 10:34

0 Answers0