[英]Detecting Key Events
我在嘗試檢測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的x64版本,但它與32位應用程序兼容。
如果要創建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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.