0

I am not an experienced developer, do not judge strictly

I am writing a short application in NASM assembly language, I managed to make the click click of the CapsLock keyboard, but the problem is that after emulating pressing CapsLock, it holds it and does not release,

I need to add a command so that after pressing the CapsLock key, it releases it after that

section .text
global _start
extern ExitProcess
extern keybd_event

_start:

    push 0   
    push 0x14 
    call keybd_event 

    push 1      
    push 0x14     
    call keybd_event 
      
    call ExitProcess 

I compile this code in CMD with these commands

nasm -f win32 mouse.asm -o mouse.obj

golink /entry:_start /console kernel32.dll user32.dll mouse.obj

I still couldn't fix this error of releasing the CapsLock key, so after launching this exe (mouse.exe) I need to press CapsLock so that it releases this key

1
  • 2
    That's not how you use keybd_event, which is also deprecated. Write your program in C first, if you need to. Commented Mar 23 at 15:01

1 Answer 1

2

Here’s a 32-bit NASM Windows program using SendInput that:

  1. Sends a full "press" (keydown + keyup) to turn CapsLock on,

  2. Waits 1 second,

  3. Sends another full "press" to turn CapsLock off,

  4. Exit program.

; caps.asm
; call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars32.bat" x86
; nasm -f win32 caps.asm -o caps.obj
; link caps.obj user32.lib kernel32.lib /SUBSYSTEM:CONSOLE /ENTRY:start /MACHINE:X86

%define VK_CAPITAL      0x14
%define KEYEVENTF_KEYUP 0x0002

section .data

; typedef struct tagINPUT {
;     DWORD   type;          // 4 bytes
;     union {
;         MOUSEINPUT    mi;  // 24 bytes on x86
;         KEYBDINPUT    ki;  // 16 bytes on x86
;         HARDWAREINPUT hi;  //  8 bytes on x86
;     };
; } INPUT;

inputDown:
    dd 1               ; INPUT_KEYBOARD
    dw VK_CAPITAL      ; wVk
    dw 0               ; wScan
    dd 0               ; dwFlags = key-down
    dd 0               ; time
    dd 0               ; dwExtraInfo
    dd 0               ; padding (2 dd to reach max 28 bytes = sizeof(INPUT))
    dd 0

inputUp:
    dd 1
    dw VK_CAPITAL
    dw 0
    dd KEYEVENTF_KEYUP ; key-up
    dd 0
    dd 0
    dd 0
    dd 0

sizeof_INPUT equ inputUp - inputDown

section .text
    global start
    extern _SendInput@12
    extern _Sleep@4
    extern _ExitProcess@4

start:
    ; --- PRESS to turn ON CapsLock ---
    push    sizeof_INPUT   ; cbSize
    push    inputDown      ; pInputs
    push    1              ; cInputs
    call    _SendInput@12

    push    sizeof_INPUT
    push    inputUp
    push    1
    call    _SendInput@12

    ; --- WAIT 1 second ---
    push    1000
    call    _Sleep@4

    ; --- PRESS again to turn OFF CapsLock ---
    push    sizeof_INPUT
    push    inputDown
    push    1
    call    _SendInput@12

    push    sizeof_INPUT
    push    inputUp
    push    1
    call    _SendInput@12

    ; --- EXIT ---
    push    0
    call    _ExitProcess@4

References

how to use sendinput function C++

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.