程式語言 - Flat Assembler (FASM) - Win32 API - Keyboard Event



參考資訊:
https://board.flatassembler.net/

WM_KEYDOWN、WM_KEYUP是一般鍵盤按鍵事件,而WM_SYSKEYDOWN、WM_SYSKEYUP則是屬於系統按鍵的事件,系統按鍵就是左上角小視窗圖案的那些按鍵命令

WM_KEYDOWN、WM_KEYUP

nVirtKey = (int) wParam; // virtual-key code
lKeyData = lParam;       // key data

WM_SYSKEYDOWN、WM_SYSKEYUP

nVirtKey = (int) wParam; // virtual-key code
lKeyData = lParam;       // key data

main.asm

    include "head.asm" 

    section ".text" code readable executable
proc WndProc hWnd, uMsg, wParam, lParam
    mov eax, [uMsg]
    cmp eax, WM_KEYDOWN
    je .handle_key
    cmp eax, WM_SYSKEYDOWN
    je .handle_key
    cmp eax, WM_CLOSE
    je .handle_close
    cmp eax, WM_DESTROY
    je .handle_destroy
    invoke CallWindowProc, [pDefWndProc], [hWnd], [uMsg], [wParam], [lParam]
    jmp .finish
 
.handle_key:
    invoke wsprintf, szBuf, FMT_KBD, [wParam]
    invoke SetWindowText, [hWnd], szBuf
    xor eax, eax
    jmp .finish
 
.handle_close:
    invoke DestroyWindow, [hWnd]
    xor eax, eax
    jmp .finish
  
.handle_destroy:
    invoke PostQuitMessage, 0
    xor eax, eax
    jmp .finish
   
.finish:
    ret
endp
  
proc WinMain hInst, hPrevInst, CmdLine, CmdShow
    local msg:MSG
  
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, szName, \
        WS_OVERLAPPEDWINDOW or WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL
    mov [hWin], eax
  
    invoke SetWindowLong, [hWin], GWL_WNDPROC, WndProc
    mov [pDefWndProc], eax
  
@@:
    lea eax, [msg]
    invoke GetMessage, eax, NULL, 0, 0
    cmp eax, 0
    je @f
    lea eax, [msg]
    invoke DispatchMessage, eax
    jmp @b
@@:
    mov eax, [msg.wParam]
    ret
endp
  
start:
    invoke GetModuleHandle, NULL
    mov [hInstance], eax
   
    invoke GetCommandLine
    mov [pCommand], eax
   
    stdcall WinMain, [hInstance], NULL, [pCommand], SW_SHOWNORMAL
    invoke ExitProcess, eax

Line 18~19:將按鍵數值顯示在視窗標題

完成