D
D
DmItrENub2021-12-26 11:49:32
C++ / C#
DmItrENub, 2021-12-26 11:49:32

How to hold down keys on the keyboard in C++?

How to create a keypress in C++? I do this code and it just presses and immediately releases the key:

#include <iostream>
#include <windows.h>

int main()
{
    keybd_event(0x57, 0, 0, 0);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hemul GM, 2021-12-27
@DmItrENub

Here's how in Delphi

procedure SimulateKeyDown(Key: Byte);
begin
  keybd_event(Key, 0, 0, 0);
end;

procedure SimulateKeyUp(Key: Byte);
begin
  keybd_event(Key, 0, KEYEVENTF_KEYUP, 0);
end;

procedure SimulateKeystroke(Key: Byte; Extra: DWORD);
begin
  keybd_event(Key, extra, 0, 0);
  keybd_event(Key, extra, KEYEVENTF_KEYUP, 0);
end;

I think everything is clear here, api is the same thing.
If there is no constant, then hereKEYEVENTF_KEYUP (0x0002)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question