C++ Simulating key presses only once - c++

I have tried everything I found in google but nothing works. I want to press A for 5 seconds, but everything presses it only once.It just releases it immediately. Pressing multiple times is not the answer because the key must be pressed for atleast 30ms.
const int KEYEVENT_KEYUP = 0x02;
keybd_event(0x41, 0, 0, 0);
Sleep(5000);
keybd_event(0x41, 0, KEYEVENT_KEYUP, 0);

Related

PostMessage / SendMessage without interference from modifier keys

I'm trying to post/send a message to some hwnd without the interference from modifier keys (ctrl, alt, shift).
Basically i want to send F1 message (without ctrl) to hwnd while im pressing ctrl (like Example 2) but with SendMessage\PostMessage.
I tried use SendInput to set up the CTRL key, post the message and set down the CTRL key back, but it fails 50% of time.
Example 1: Code with SendMessageA:
HWND hwnd = FindWindowA(0, "Notepad");
if (GetKeyState(VK_CONTROL) < -1) // if CTRL is pressed
{
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); // up the CTRL key
SendMessageA(hwnd, WM_KEYDOWN, VK_F1, 0); // send F1 keydown
keybd_event(VK_CONTROL, 0, 0, 0); // down the CTRL key
}
else
{
SendMessageA(hwnd, WM_KEYDOWN, VK_F1, 0); // send F1 keydown
}
Theoretically this code would solve the problem, but it sometimes sends the message with the CTRL pressed and sometimes not.
Example 2: Same code with SendInput (but these works fine)
HWND hwnd = FindWindowA(0, "Notepad");
if (GetKeyState(VK_CONTROL) < -1) // if CTRL is pressed
{
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); // up the CTRL key
keybd_event(VK_F1, 0, 0, 0); // send F1 keydown
keybd_event(VK_CONTROL, 0, 0, 0); // down the CTRL key back
}
else
{
keybd_event(VK_F1, 0, 0, 0); // send F1 keydown
}

auto click with C++

I write a simple cpp file when your left mouse button down it will click after 50 milliseconds and it works with many windows but when I click in the Tencent Gaming Buddy(an android emulator) it is not working - so how could I get the left mouse button down when I click here is my code
while (true)
{
Sleep(50);
if ((GetKeyState(VK_LBUTTON) & 0x80) != 0)
{
if (GetCursorPos(&p))
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
}
}
if ((GetKeyState(VK_RBUTTON) & 0x80) != 0)
{
break;
}
}
GetKeyState() relies on the internal key state machine of the calling thread, and does not work without an active message loop to update that state, which this code does not have. Use GetAsyncKeyState() instead.
Also, mouse_event() is deprecated, use SendInput() instead

PostMessage not clicking buttons

I've played with PostMessage just clicking different tabs which seemed to work. But I'm trying to automate some button clicks and when running this function it highlights the button as if I was hovering over it but doesn't click.
I thought somehow the button changing of colour made the boolean false so I added the exception of the buttons colour whilst it's hovered. Made no difference, and I do not wish to use SetCursorPos & simulate a mouseclick using SendInput. I wish to understand the problem/issue I'm having as to why it's not clicking.
void click(const std::vector<uint_16>& x, const uint_16& y)
{
for(uint_8 i = 0; i < 5; i++)
{
if(content::MyClass().firstMatch(GetPixel(hdc, x[i], y)))
{
PostMessage(hwnd, WM_LBUTTONDOWN, 0, MAKELPARAM(x[i], y));
return;
}
}
if(content::MyClass().secondMatch(GetPixel(hdc, x[4], y)))
{
PostMessage(hwnd, WM_LBUTTONDOWN, 0, MAKELPARAM(x[4], y));
}
}
The solution you are using is unreliable because you are short-circuiting the input system on the window and not specifically targeting which button you are trying to press.
As for the reason your code is not currently working, you only send a WM_LBUTTONDOWN message to the window. Since most buttons work off a combination of WM_LBUTTONDOWN and WM_LBUTTONUP your program isn't causing the buttons click method to activate.
Adding PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(x[i], y)); after the mouse down will cause the button click to register.
In future as a more reliable solution that will specifically target a button on the window and click it you may want to look at the BM_CLICK PostMessage argument. Using this instead of trying to emulate a mouse click is more correct because windows will trigger events that may otherwise be forgotten when using the mouse down and mouse up post commands.
An example:
int retVal = 0;
HANDLE hwndDialog;
HANDLE hwndButton;
/* First, see if the dialog box (titled "Inactivity Warning" ) is currently open. */
hwndDialog = FindWindow( 0, "Inactivity Warning" );
if ( hwndDialog == 0 ) return;
/* Now get a handle to the "Resume" button in the dialog. */
hwndButton = FindWindowEx( hwndDialog, 0, 0, "Resume" );
/* After making sure that the dialog box is the active window, click "Resume". */
retval = SetActiveWindow( hwndDialog );
/* converted from SendMessage. */
retval = PostMessage( hwndButton, BM_CLICK, 0, 0 );
Source found Here, Converted from VB by me.
For some further reading on the input system Here is a good article.
A Blog Post by Raymond Chen goes into a bit of detail about these commands and their caveats as well.

Programmaticaly simulating Alt + Enter key press is not working

Here is my code:
keybd_event(VK_MENU, 0, 0, 0);
keybd_event(VK_RETURN, 0, 0, 0);
Sleep(200);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
The first line would press Alt
The second line would press Enter ↵ (or Return ↵),
The fourth line would release Alt,
The fifth line would release Enter ↵ (or Return ↵).
You are not setting the KEYEVENTF_EXTENDEDKEY flag to keep the keys pressed down. Change your code to:
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_EXTENDEDKEY, 0);
Sleep(200);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
Also you really don't need the sleep in the middle if you are just sending a Alt + Enter
You can see all of the keycodes here at the MSDN page.
Alt = VK_MENU
Left Alt = VK_LMENU
Right Alt Gr = VK_RMENU

RegisterHotKey without a modifier in c++

This piece of code registers 2 global hotkeys under Windows for SHIFT+F5 and SHIFT+F6 it works fine in all cases, including the case that a completely other application has the focus like for example a game.
enum{ KEY_F5 = 1, KEY_F6 = 2 };
RegisterHotKey(0, KEY_F5, MOD_SHIFT, VK_F5);
RegisterHotKey(0, KEY_F6, MOD_SHIFT, VK_F6);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
PeekMessage(&msg, NULL, 0, 0, 0);
switch (msg.message){
case WM_HOTKEY:
if (msg.wParam == KEY_F5){
// code
}
else if (msg.wParam == KEY_F6){
// code
}
}
}
But if i replace
RegisterHotKey(0, KEY_F5, MOD_SHIFT, VK_F5);
RegisterHotKey(0, KEY_F6, MOD_SHIFT, VK_F6);
by
RegisterHotKey(0, KEY_F5, 0, VK_F5);
RegisterHotKey(0, KEY_F6, 0, VK_F6);
because my goal is it two have F5 and F6 as hotkeys without having to press SHIFT always, it only works if my program has the focus not if some other has. How can i achieve to have only F? as global hotkey like for example TS3 does. It must work even if the application doesn't have the focus.
Try use MOD_NOREPEAT instead of MOD_SHIFT. I also don't see any necessity of using "PeakMessage". Since you already call GetMessage(), PeakMessage is really redundant.