-2

The "processKey" function below is made to recognize what key you have pressed on the keyboard and perform some action based on which key was pressed. All of the keys that are programmed below work. I need to add in to the function the ability to detect the four arrow keys (up, down, left, right) as well as CTRL UP and CTRL DWN. I understand that there is no Ascii code for these keys and that there are scan codes. Could some body please help me find the scan codes? I have tried looking online but haven't had much luck. If it has anything to do with the operating system I am running Windows 8.1 on my Macintosh. Thank you!

processKey PROC
    PUSH AX BX
    MOV AH, 10h
    INT 16h
    CMP AL, 1Bh                     ;ESC key?
    JE ESCKey
    JMP F1Key
 ESCKey:
    MOV     AH, 4Ch                
    INT     21h
 F1Key:
    CMP AX, 3B00h                   ;F1 key?
    JE toggleLineType
    JMP F2Key
 toggleLineType:
    CMP singleOrDouble, 0
    JE toggleDouble
    MOV singleOrDouble, 0
    JMP done
 toggleDouble:
    MOV singleOrDouble, 1
    JMP done
 F2Key:
    CMP AX, 3C00h                   ;F2 key?
    JE foregroundToggle
    JMP F3Key
 foregroundToggle:
    CMP foreground, 7
    JE resetForeground
    MOV BL, foreground
    INC BL
    MOV foreground, BL
    JMP done
 resetForeground:
    MOV foreground, 1
 F3Key:
    CMP AX, 3D00h                   ;F3 key?
    JE backgroundToggle
    JMP done
 backgroundToggle:
    CMP background, 7
    JE resetBackground
    MOV BL, background
    INC BL
    MOV background, BL
    JMP done
 resetBackground:
    MOV background, 1
 done:
    POP BX AX
    CALL drawBox
 RET   
processKey ENDP
BCRwar1
  • 189
  • 5
  • 21
  • Why not just try what code you get when you press an arrow key? Using a debugger or logging. – Jester Apr 08 '15 at 23:35
  • I'm new to all this and I don't know how to do that, its for my class – BCRwar1 Apr 08 '15 at 23:36
  • _"I am running Windows 8.1 on my Macintosh"_ I doubt that you're running this directly in Windows, though. I'm guessing that you're using DOSBox or something similar. As Jester suggested, just print all the scancodes you read and watch what gets printed when you press the arrow keys. – Michael Apr 09 '15 at 07:19
  • Actually I am running it directly in windows, through boot camp, and I don't know how to do that – BCRwar1 Apr 09 '15 at 16:31

1 Answers1

0

I copy-pasted your code and added the necessary to make it work with my EMU8086 (some variables declared, empty proc draw_box). First, I added the compares with the arrow codes, now the tricky part : in UP and DOWN arrows (labels arrow_up:, arrow_down:) we get the state of the keyboard flags (ctrl, shift, caps lock, scroll lock, etc), and, if UP arrow was pressed and the Ctrl key is pressed, we have Ctrl+Up (or Ctrl+Down), and jump to labels ctrl_up: or ctrl_down:. Here is the code :

.stack 100h
.data
singleOrDouble DB 0
foreground     DB 0
background     DB 0
.code          
;INITIALIZE DATA SEGMENT.
  mov  ax,@data
  mov  ds,ax

  call processKey

;FINISH THE PROGRAM.
  mov  ax,4c00h
  int  21h           

;--------------------------------------------

processKey PROC
    PUSH AX
    PUSH BX
    MOV AH, 10h
    INT 16h

    cmp ax, 4800h   ;UP.
    je  arrow_up
    cmp ax, 5000h   ;DOWN.
    je  arrow_down
    cmp ax, 4B00h   ;LEFT.
    je  arrow_left
    cmp ax, 4D00h   ;RIGHT.
    je  arrow_right
    cmp ax, 00h
    je  arrow_right
    jmp chkEsc
arrow_up:          
;CHECK IF LEFT CTRL IS PRESSED.
    mov ah, 12h ;GET STATE OF CAPS LOCK, SHIFT, ETC.
    int 21h                                         
    and al, 00000100b ;BITS FOR LEFT CTRL.
    cmp al, 0
    jne ctrl_up
;DO SOMETHING.
    jmp done
ctrl_up:
;DO SOMETHING.
    jmp done
arrow_down:
;CHECK IF LEFT CTRL IS PRESSED.
    mov ah, 12h ;GET STATE OF CAPS LOCK, SHIFT, ETC.
    int 21h                                         
    and al, 00000100b ;BITS FOR LEFT CTRL.
    cmp al, 0
    jne ctrl_down
;DO SOMETHING.
    jmp done
ctrl_down:
;DO SOMETHING.
    jmp done
arrow_left:
;DO SOMETHING.
    jmp done
arrow_right:
;DO SOMETHING.
    jmp done

chkEsc:    
    CMP AL, 1Bh                     ;ESC key?
    JE ESCKey
    JMP F1Key
 ESCKey:
    MOV     AH, 4Ch                
    INT     21h
 F1Key:
    CMP AX, 3B00h                   ;F1 key?
    JE toggleLineType
    JMP F2Key
 toggleLineType:
    CMP singleOrDouble, 0
    JE toggleDouble
    MOV singleOrDouble, 0
    JMP done
 toggleDouble:
    MOV singleOrDouble, 1
    JMP done
 F2Key:
    CMP AX, 3C00h                   ;F2 key?
    JE foregroundToggle
    JMP F3Key
 foregroundToggle:
    CMP foreground, 7
    JE resetForeground
    MOV BL, foreground
    INC BL
    MOV foreground, BL
    JMP done
 resetForeground:
    MOV foreground, 1
 F3Key:
    CMP AX, 3D00h                   ;F3 key?
    JE backgroundToggle
    JMP done
 backgroundToggle:
    CMP background, 7
    JE resetBackground
    MOV BL, background
    INC BL
    MOV background, BL
    JMP done
 resetBackground:
    MOV background, 1
 done:
    POP BX
    POP AX
    CALL drawBox
 RET   
processKey ENDP 

proc drawBox
 RET
endp