Windows 检测关键事件

Windows 检测关键事件,windows,assembly,fasm,Windows,Assembly,Fasm,我在尝试检测x86汇编语言中的关键事件时遇到问题。当我运行我的程序时,会出现以下一般错误: key.exe遇到问题,需要关闭。给您带来不便,我们深表歉意 我的汇编程序fasm生成一个.bin文件、一个.exe文件和一个.com文件。如果我尝试运行.com文件,会弹出一个消息框,说明该映像文件有效,但适用于当前机器以外的机器类型 这是我的密码: include 'include/win32ax.inc' section '.data' data readable writeable inchar

我在尝试检测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

我使用的是x64版本的windows xp,但它与32位应用程序兼容。

如果您正在创建Win32程序,则不能使用DOS API(int 21h)来获取按键

您应该使用函数并检查键盘事件

以下是如何做到这一点:

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

看起来您正在编译一个DOS.COM应用程序,其中包含32位Windows特定代码。那不行。@AlexeyFrunze我能举一个什么有效的例子吗?当我在谷歌上搜索汇编键事件时,我只得到int 21h这个东西。没有控制台我怎么能做到这一点?你可以创建一个窗口,它(聚焦时)将接收键盘消息。