2013-04-13 144 views
0

我在嘗試檢測x86彙編語言中的關鍵事件時遇到了問題。當我運行我的程序時,出現此通用錯誤:檢測關鍵事件

key.exe遇到問題並需要關閉。對此造成的不便,我們表示歉意。

我的彙編程序fasm生成.bin文件,.exe文件和.com文件。如果我嘗試運行.com文件,會彈出一個消息框,提示圖像文件是有效的,但用於除當前機器以外的機器類型。

這裏是我的代碼:

include 'include/win32ax.inc' 
section '.data' data readable writeable 

inchar  DB ? 
numwritten DD ? 
numread DD ? 
outhandle DD ? 
inhandle DD ? 
char DB ? 

    section '.text' code readable executable 
    start: 

    ;set up the console 
invoke AllocConsole 
invoke GetStdHandle,STD_OUTPUT_HANDLE 
mov [outhandle],eax 
invoke GetStdHandle,STD_INPUT_HANDLE 
mov [inhandle],eax 

    ;get key press 
mov ah,1h 
int 21h 
mov [char],AL 

    ;print out the key pressed 
invoke WriteConsole,[outhandle],char,15,numwritten,0 
invoke ReadConsole,[inhandle],inchar,1,numread,0 
invoke ExitProcess,0 

    .end start 

我使用Windows XP的64位版本,但它與32位應用程序兼容。

+2

你看上去編譯在其32位Windows的特定代碼DOS .COM程序。這是行不通的。 –

+0

@AlexeyFrunze我可以舉一個例子來說明什麼會起作用嗎?當我谷歌大會關鍵事件,我只是得到int 21h的東西 – user2097804

回答

1

如果您正在創建Win32程序,則無法使用DOS API(int 21h)來獲取按下的鍵。

您應該使用ReadConsoleInput函數並檢查鍵盤事件。

這裏是如何可以做到這一點:

include '%fasminc%/win32ax.inc' 
section '.data' data readable writeable 

struc KEY_EVENT_RECORD { 
    .fKeyDown  dd ? 
    .Repeat   dw ? 
    .VirtKeyCode dw ? 
    .VirtScanCode dw ? 
    .res   dw ? 
    .char   dd ? 
    .ctrl   dd ? 
} 

struc INPUT_RECORD { 
    .type dw ? 
    .event KEY_EVENT_RECORD 
} 

KEY_EVENT = 1 

inchar  DB ? 
numwritten DD ? 
numread DD ? 
outhandle DD ? 
inhandle DD ? 

input  INPUT_RECORD 
count  dd ? 


section '.text' code readable executable 

start: 

     ;set up the console 
     invoke AllocConsole 
     invoke GetStdHandle,STD_OUTPUT_HANDLE 
     mov [outhandle],eax 
     invoke GetStdHandle,STD_INPUT_HANDLE 
     mov [inhandle],eax 


.loop: 
     invoke ReadConsoleInput, [inhandle], input, 1, count 
     cmp  [count], 1 
     jne  .loop 
     cmp  [input.type], KEY_EVENT 
     jne  .loop 

      ;print out the key pressed 
     invoke WriteConsole,[outhandle],input.event.char,1,numwritten,0 
     invoke ReadConsole,[inhandle],inchar,1,numread,0 
     invoke ExitProcess,0 

.end start 
+0

我怎麼能沒有控制檯做到這一點? – user2097804

+0

您可以創建窗口,(聚焦時)將接收鍵盤消息。 – johnfound