简体   繁体   English

检测关键事件

[英]Detecting Key Events

I am having trouble trying to detect key events in the x86 assembly language. 我在尝试检测x86汇编语言中的关键事件时遇到了麻烦。 When I run my program, I get this generic error: 运行程序时,出现以下一般错误:

key.exe has encountered a problem and needs to close. key.exe遇到问题,需要关闭。 We are sorry for the inconvenience. 我们对造成的不便很抱歉。

fasm, my assembler, generates a .bin file, a .exe file, and a .com file. fasm,我的汇编程序,生成一个.bin文件,.exe文件和.com文件。 If I try running the .com file, a message box pops up saying that the image file is valid, but is for a machine type other than the current machine. 如果我尝试运行.com文件,则会弹出一个消息框,提示该图像文件有效,但适用于当前计算机以外的其他计算机类型。

Here is my code: 这是我的代码:

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

I am using a x64 edition of windows xp, but it is compatible with 32-bit applications. 我正在使用Windows XP的x64版本,但它与32位应用程序兼容。

If you are creating Win32 program, you can't use DOS API ( int 21h ) in order to get pressed keys. 如果要创建Win32程序,则不能使用DOS API(int 21h)来获得按下的键。

You should use ReadConsoleInput function and check for keyboard events. 您应该使用ReadConsoleInput函数并检查键盘事件。

Here is how this can be done: 这是可以做到的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM