programmers resources
  http://www.intel-assembler.it/  (c)2017 intel-assembler.it   info@intel-assembler.it
 
Search :  
Lingua Italiana    English Language   
Index
 
just an empty assembly space
just an arrow Intel Platform
just an arrow Article & Guides
just an arrow Download Software


23/01/2009 Featured Article: How to remove Buzus Virus (permalink)




:::3058431:::
Bottone Scambio Directory Pubblicitaonline.it
Home Page | Articles & Guides | Download | Intel Platform | Contacts

Google
 


Bookmark and Share
Download 
Tell a friend



Assembler Keyboard Handler INT9 IRQ1

An assembly source code for keyboard handling

(by hamell@cs.pdx.edu)

An assembly source code for creating your own INT9 (which corresponds to IRQ1) software handler.
The code intercepts keys being pressed, including special keys like function keys and handles the event.
This article is online from 2656 days and has been seen 6974 times




INT9 (IRQ1) keyboard handler #9 by Patch (hamell@cs.pdx.edu)
===============================================================================
                                                                June 15th, 1994

Added a variable called KEYLAST so you could do input and build a string with
the values in this variable.  toASCII is a translate table that it gets shifted
and unshifted characters from and stores the result in KEYLAST.  The #define's
that start with b are for BIOS equivalents (most of them are).  To get input
in the normal manner of getch(), call GetKey.  The first key available from the
keyboard buffer is stored in KEYLAST (0 means no key available).

You can change the toASCII table to match the hardware scan codes to ASCII
codes.  As it is now only alphanumeric, Enter, and ESC are translated.  Keys
such as F1-F12, Insert, etc. are translated to return 0.

Description of variables/functions/files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
keys[]         - Array of 256 bytes.  Each position corresponds to a key on the 
                 keyboard.  A value of 1 means the key is being pressed, 0 means
 
                 released.  If you wanted to check for the LEFT SHIFT key being
                 pressed, you'd type
                   if (keys[kLEFTSHIFT] == 1) ...
keynumpress    - Tells you the number of keys being pressed at any given time
                 If you want to do something while waiting for a keypress, you 
                 could do
                           while (keynumpress == 0) ;
                 or
                           do GetKey(); while (keylast == 0);
                 instead of
                           while (!kbhit());
keylast        - shifted value of last key hit
Set_New_Int9() - Call this function when you want to install the INT 9 handler
Set_Old_Int9() - Call this function when you want to uninstall the INT 9 handler
ClearKeys      - clears the keyboard buffer
GetKey         - returns the first key from the keyboard buffer in the variable
                 KEYLAST (0 means no key available)
KEY.DOC        - this doc file
KEY.ASM        - ASM source for the INT9 handler (32-bit)
KEY16.ASM      - ............................... (16-bit)
KEY.H          - C #include header file (32-bit)
KEY16.H        - ...................... (16-bit)
KEY.OBJ        - compiled KEY.ASM
KEY16.OBJ      - compiled KEY16.ASM
KEYTEST.C      - C source for a program testing the INT9 handler
KEYTEST.EXE    - executable (DOS/4GW required to run it)

If you and use it (whether it be as-is or slightly modified), please credit me.
Please email me any changes you make so I can include them in future releases.

To get more similar source and/or grafx/sound source, call my board:

        Dead Man's Hand
        (503) 288-9264
        USR 28.8k DS
        Grafx/sound programming and VGA demos
        
        
;******************************************************************************
;* INT9 keyboard handler #9 (16-bit)
;* by Lee Hamel (hamell@cs.pdx.edu)
;* June 15th, 1994
;*
;* All keyboard buffering code by James Ketrenos (ketrenoj@cs.pdx.edu)
;******************************************************************************
.model large
.286
JUMPS

PIC_CMD                 EQU     20h
NONSPEC_EOI             EQU     20h

.code
PUBLIC  _keys
        _keys           db      256 dup (0)
PUBLIC  _keynumpress
        _keynumpress    db      0
PUBLIC  _keylast
        _keylast        db      0
PUBLIC  _oldkeylast
        _oldkeylast     db      0

toASCII                 db      0,27,'1234567890-=',14,15
                        db      'qwertyuiop',0,0,13,0,'as'
                        db      'dfghjkl',0,0,'''',0,0,'zxcv'
                        db      'bnm',0,'./',0,0,0,' ',0,1,2,3,4,5
                        db      6,7,8,9,10,0,0,24,25,26,'-',21,22,23,0,18
                        db      19,20,16,17,0,0,0,11,12,0,0,0,0,0,0,0
                        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
                        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
                        db      0,27,'!',0,'#',0,'%',0,0,0,0,0,0,0,14,15
                        db      'QWERTYUIOP',0,0,13,0,'AS'
                        db      'DFGHJKL:',0,'"',0,0,'ZXCV'
                        db      'BNM',0,0,'?',0,0,0,' ',0,1,2,3,4,5
                        db      6,7,8,9,10,0,0,24,25,26,'-',21,22,23,0,18
                        db      19,20,16,17,0,0,0,11,12,0,0,0,0,0,0,0
                        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
                        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
e0flag                  db      0
label oldint9 dword
oldint9off              dw      ?
oldint9seg              dw      ?
keyinst                 db      0
keyhead                 db      0
keytail                 db      0
keybuffer               db      256 dup(0)
keyrepeat               db      256 dup(0)

;*****************************************************************************
;
; ClearKeys and GetKey routines by James Ketrenos (ketrenoj@cs.pdx.edu)
;
;*****************************************************************************

                        PUBLIC  _ClearKeys
_ClearKeys              PROC    FAR
                push    bx
        ;; *********************************************
        ;; Clear the buffer by setting TAIL on HEAD
        ;; *********************************************
                mov     bl,keyhead
                mov     keytail,bl
                mov     [_keylast],0
                pop     bx
                ret
_ClearKeys              ENDP

                        PUBLIC  _GetKey
_GetKey                 PROC    FAR
                push    ax bx
                xor     ax,ax
                xor     bx,bx
        ;; *********************************************
        ;; Check to see if buffer is EMPTY
        ;; *********************************************
        mov     bl,keytail              ;; Point to the end of the buffer
        cmp     bl,keyhead              ;; : and if it is equal to the front,
        jz      GKDone                  ;; : then the buffer is empty, so wait.

        ;; *********************************************
        ;; Get next key from KEYBOARD BUFFER
        ;; *********************************************
        mov     al,keybuffer[bx]        ;; Fetch the ASCII Code
        inc     [keytail]               ;; Increment the KeyTail
                                        ;; : NOTE that it loops at bl==256
                cmp     al,1Bh          ; Esc
                jne     GKDone
                cmp     keyrepeat[1],2  ; dont want Esc repeating
                jb      GKDone
                xor     al,al
GKDone:
                mov     [_keylast],al
                pop     bx ax
                ret                     ;; Return to caller and all that stuff
_GetKey                 ENDP

                        PUBLIC  _Set_New_Int9
_Set_New_Int9           PROC    FAR
                cmp     [keyinst],1
                je      exitnew9

                mov     [keyinst],1
                cli
                pusha
                push    ds es
                mov     ax,3509h
                int     21h
                mov     [oldint9off],bx
                mov     [oldint9seg],es
                mov     ax,2509h
                mov     dx,offset New_Int9
                push    cs
                pop     ds
                int     21h
                pop     es ds
                popa
                sti
exitnew9:       ret
_Set_New_Int9           ENDP

                        PUBLIC  _Set_Old_Int9
_Set_Old_Int9           PROC    FAR
                cmp     [keyinst],0
                je      exitold9

                mov     [keyinst],0
                cli
                pusha
                push    ds
                mov     dx,[oldint9off]
                mov     ds,[oldint9seg]
                mov     ax,2509h
                int     21h
                pop     ds
                popa
                sti
exitold9:       ret
_Set_Old_Int9           ENDP

New_Int9                PROC    FAR
                push    ax bx cx ds
                mov     ax,@data
                mov     ds,ax

                in      al,60h
                mov     ah,al

; these 5 lines of code only necessary on XT's
;                in      al,61h
;                or      al,80h
;                out     61h,al
;                and     al,7Fh
;                out     61h,al

;                pushf                          ; this calls the BIOS handler
;                call    [oldint9]

                cmp     ah,0E0h
                jae     e0flagset

                xor     bx,bx
                mov     bl,ah
                and     bl,01111111b
                add     bl,[e0flag]
                mov     [e0flag],0

                rol     ah,1
                jc      keyrelease

keypress:       mov     ah,1
                sub     ah,_keys[bx]            ; old key status
                add     [_keynumpress],ah
                mov     _keys[bx],1             ; key pressed
                mov     bh,_keys[2ah]           ; get left shift status
                or      bh,_keys[36h]           ; get right shift status
                ror     bh,1                    ; put in bit 7
                add     bl,bh                   ; final key value
                xor     bh,bh                   ; clear for index use
                mov     al,toASCII[bx]          ; get translated value

        ;; *********************************************
;; Check to see if buffer is FULL
;; *********************************************
        mov     cx,bx
        xor     bx,bx
        mov     bl,keyhead              ;; Point to the front of the buffer
        inc     bl                      ;; : and if the next space places
        cmp     bl,keytail              ;; : us on the end of the buffer,
        jz      int9_done               ;; : then the buffer is full ...
        dec     bl                      ;; : otherwise, we help fill it.

;; *********************************************
;; Put key into KEYBOARD BUFFER (Adding scancode)
;; *********************************************
        mov     keybuffer[bx],al        ;; Save the ASCII Code into buffer
        inc     bl                      ;; Increment the KeyHead
        mov     keyhead,bl              ;; : NOTE that it loops at bl==256
        mov     bx,cx
        cmp     keyrepeat[bx],2
        je      int9_done
        inc     keyrepeat[bx]
                jmp     int9_done

keyrelease:
                dec     [_keynumpress]
                mov     _keys[bx],0             ; key released
                mov     keyrepeat[bx],0
                jmp     int9_done

e0flagset:      mov     [e0flag],128
int9_done:      mov     al,NONSPEC_EOI
                out     PIC_CMD,al
                pop     ds cx bx ax
                iret
New_Int9                ENDP

                END




Top
Download 
Tell a friend
Bookmark and Share



Similar Articles

Capture CTRL+ALT+DEL in assembly
Source code for capturing keys under windows
(by Daniel J. Reynolds)

Keyboard Management Library
An x86 source code for keyboard handling
(by Sean Werkema)

Programming the Keyboard
A short guide on keyboard handling in asm
(by Mark Feldman)

 Tags: keyboard


webmaster jes
writers rguru, tech-g, aiguru, drAx

site optimized for IE/Firefox/Chrome with 1024x768 resolution

Valid HTML 4.01 Transitional


ALL TRADEMARKS ® ARE PROPERTY OF LEGITTIMATE OWNERS.
© ALL RIGHTS RESERVED.

hosting&web - www.accademia3.it

grossocactus
find rguru on
http://www.twitter.com/sicurezza3/
... send an email ...
Your name

Destination email

Message

captcha! Code