2012-03-21 115 views
1

我正在學習中斷和鍵盤硬件中斷,例如中斷9(在dos中)。 我注意到,如果我按下箭頭鍵(左,右,上,下),則會出現兩個連續的中斷。第一個是'Shift'按鈕中斷,第二個是我按下的箭頭鍵。按下箭頭鍵拍攝兩個鍵盤中斷? (int 09h)

我注意到,因爲我重寫並配置了鍵盤的9號中斷來提示按下按鈕的掃描碼。例如,當我按下右箭頭鍵時,我會看到發生了'Shift'按鈕中斷(在屏幕上顯示了scane代碼42),然後是我按下的箭頭鍵(右箭頭鍵)也發送中斷(掃描碼77)。

我的問題是,爲什麼會發生這種情況?

我對INT 9代碼:

void interrupt interrupt_9_Implementation{ 

unsigned char scanCode; 

asm{ 

    in al, 60h // read the keyboard input from port 60h (96 Decimal) into al; 
    mov scanCode, al // save the keyboard input into 'scanCode' varaible 
    in al, 61h // read 8255 port 61h (97 Decimal) into al 
    or al, 128   // set the MSB - the keyboard acknowlege signal 
    out 61h, al   // send the keyboard acknowlege signal from al 
    xor al, 128 // unset the MSB - the keyboard acknowlege signal 
    out 61h, al  // send the keyboard acknowlege signal from al 
} 

if(128 > scanCode){ // if the button is being pressed or being released. if the button is being pressed then the MSb isn't set and therfore it must be smaller than 128 

    printf("You pressed key assigned scan code = %d\n", scanCode); 

    if(EscScanCode == scanCode) 
     EscPressed = _True; 
    else 
     printf("Press any key (almost)\n:"); 
} 

// send EOI 
asm{ 
    mov al, 20h 
    out 20h, al 
} 
} 

後,我按箭頭鍵(例如右箭頭鍵),我會得到:

Press any key (almost) 
:You pressed key assigned scan code = 42 // the 'shift' key scan code 
Press any key (almost) 
:You pressed key assigned scan code = 77 // the right arrow button scan code 

到目前爲止,這是唯一的發生用箭頭鍵。而'Shift'沒有被按下。 我使用的是Logitech Wave鍵盤。

回答

0

根據http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html

1.7 Added non-fake shifts 

On my 121-key Nokia Data keyboard there are function keys F1, ..., F24, where F1, ..., F12 
send the expected codes 3b, ..., 58, and F13, ..., F24 send the same codes together with 
the LShift code 2a. Thus, F13 gives 2a 3b on press, and bb aa on release. Similarly, there 
are keys with added LCtrl code 1d. But there are also keys with added fake shifts e0 2a. 

Delorie reports that the "Preh Commander AT" keyboard with additional F11-F22 keys treats 
F11-F20 as Shift-F1..Shift-F10 and F21/F22 as Ctrl-F1/Ctrl-F2; the Eagle PC-2 keyboard 
with F11-F24 keys treats those additional keys in the same way. 

這不正是你的描述,但它揭示了相當古怪的行爲的一些情況。我會說嘗試另一個鍵盤,看看會發生什麼。

(我想這屬於一個評論,而不是一個答案,但我不能看到一個評論框...)

3

你的numlock上。

您實際上並未打印您正在接收的所有掃描碼。您只在代碼少於128時打印。但是,掃描代碼前面可能會有0xE0,表示擴展代碼。

微軟對鍵盤掃描碼rather nice write-up,其具有如下描述:

    Base Make Base Break 
Right Arrow  E0 4D  E0 CD 
... 
Num Lock ON  Precede Base   follow Base Break 
        Make code with   code with 
Final Key only E0 2A     E0 AA 

那麼你實際上是接受這個按鍵順序:

E0 2A E0 4D 

由於你的代碼沒有按」打印128以上的任何東西(0xE0是224),你只能看到打印的0x2A(42)和0x4D(77)。