Skip to content

Read Keyboard

Maxime THIZEAU edited this page Dec 1, 2023 · 1 revision

Function Name: readKeyboard

Description:

This function aims to read the keyboard to move Pac-Man afterward. It look in the buffer if there is a key which was pressed and jump to the comparisons if not. If yes it stores the last key in a register. It move the high nibble to a variable call "KeyPressed". There is then a lot of comparisons for all the arrows and the escape touch. Each comparisons jump to a movement like up, down, etc. The escape comparison jump to the exit. If not ecape then ret to the main game loop.

Parameters:

  • KeyPressed : word, Keep the last scancode's key
  • UP_KEY_SCANCODE : hexadecimal value, up arrow's scancode
  • DOWN_KEY_SCANCODE hexadecimal value, down arrow's scancode
  • LEFT_KEY_SCANCODE : hexadecimal value, left arrow's scancode
  • RIGHT_KEY_SCANCODE : hexadecimal value, right arrow's scancode
  • EXIT_KEY_SCANCODE : hexadecimal value, escape touch's scancode

Return Type:

This function is only here to read the keyboard and to move Pac-Man or exit the game afterwards. So there is no value which is return.

Usage Example:

%define UP_KEY_SCANCODE 48h
%define DOWN_KEY_SCANCODE 50h
%define LEFT_KEY_SCANCODE 4bh
%define RIGHT_KEY_SCANCODE 4dh
%define EXIT_KEY_SCANCODE 01h

readKeyboard:
    ; Don't do anything if no key was pressed
    mov ah, 01h
    int 16h
    jz .skipBufferRead

    ; Read last key in buffer:
    .keepReadingBuffer:
        int3
        mov ah, 00h
        int 16h
        mov bx, ax
        mov ah, 01h
        int 16h
        jnz .keepReadingBuffer

    ; Overwrite the first char in 'charDump' with received char:
    mov [keyPressed], bh

    .skipBufferRead:

        ; Exit if ESCAPE
        cmp byte [keyPressed], EXIT_KEY_SCANCODE
        je exit

        ; Left
        cmp byte [keyPressed], LEFT_KEY_SCANCODE
        je MoveLeft

        ; Right
        cmp byte [keyPressed], RIGHT_KEY_SCANCODE
        je MoveRight

        ; Up
        cmp byte [keyPressed], UP_KEY_SCANCODE
        je MoveUp

        ; Down
        cmp byte [keyPressed], DOWN_KEY_SCANCODE
        je MoveDown

    ret

Important Notes:

  • None

Modifications History:

  • Using Scancode, 2023/11/22, we can't use arrow's with Ascii code, Léo CHARTIER / Maxime THIZEAU

Test Cases:

  • Left arrow is pressed, Pac-Man move to left.
  • right arrow is pressed, Pac-Man move to right.
  • Up arrow is pressed, Pac-Man move up.
  • Down arrow is pressed, Pac-Man move down.
  • Escape is pressed, the game close.

Additional Documentation:

Author:

  • Maxime THIZEAU