-2

下面的“processKey”功能用于识别您在键盘上按下的键,并根据按下的键执行一些操作。下面编程的所有键都可以工作。我需要在函数中添加检测四个箭头键(上、下、左、右)以及 CTRL UP 和 CTRL DWN 的功能。我知道这些键没有 Ascii 代码,并且有扫描代码。有人可以帮我找到扫描码吗?我曾尝试在网上查找,但运气不佳。如果它与我在 Macintosh 上运行 Windows 8.1 的操作系统有关。谢谢!

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
4

1 回答 1

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 
于 2015-04-09T19:16:38.320 回答