0

Im making A program in which c++ presses keys on the keyboard automatically for me. I have done this with every key on the keyboard. It works perfectly EXCEPT for when i am trying to use square brackets '['. If i tell the computer to print out a square bracket, it prints out the square bracket as expected, but it also presses the windows start button, therefore opening the start menu? After some debugging i found that the start menu only popped up after the SendInput function was used to RELEASE THE KEY not when it was pressed. So my question is WHY is this happening? PS Before anyone asks, my code is 100% correct for every key so i know the method i am using works.

ip.ki.wVk = VK_OEM_4; // this is the '[' key
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT)); // the key has been pressed

ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT)); // the key is released, THE START MENU POPS UP!
noobcoder
  • 6,089
  • 2
  • 18
  • 34
  • Are you using WinAPI ? And maybe you should put key-press event too, windows needs it for key release – Alker Feb 26 '14 at 10:30
  • These are the vagaries of poking keystrokes, you'll easily become a victim of keyboard state. Pressing Ctrl+[ is a way to generate the Escape control code. And Ctrl+Escape activates the Start menu. Something like that. – Hans Passant Feb 26 '14 at 12:22
  • More code please. Maybe the struct isn't set up right. – david.pfx Feb 26 '14 at 13:04

1 Answers1

2

I cannot reproduce this, here is my minimal code:

#include <Windows.h>

int main() {
    INPUT ip = {0};

    ip.type = INPUT_KEYBOARD;
    ip.ki.wVk = VK_OEM_4;

    Sleep(2000);

    SendInput(1, &ip, sizeof ip);

    ip.ki.dwFlags = KEYEVENTF_KEYUP;

    // Note: Adding some kind of delay here might be a good idea.

    SendInput(1, &ip, sizeof ip);

    return 0;
}

Check if this works for you to see if it is an issue with your program or some kind of keyboard layout issue.

This correctly presses [ on my PC and doesn't open start menu, but please note that it depends on keyboard layout mapping VK_OEM_4 to [ which might not be the case on some layouts. You could use VkKeyScanEx to translate a character to virtual-key code.

Some guesses as to why your program behaves as you described: maybe you are not zeroing your INPUT structure (with = {0} or memset) and it contains some trash values, or you are accidentally simulating some kind of key combination that happens to open start menu, or maybe there is some other mistake in your code but that is impossible to tell because you didn't show complete example, there are many possibilities.

Also, if you want to send characters, using Unicode values might be more portable:

#include <Windows.h>

int main() {
    INPUT ip = {0};

    ip.type = INPUT_KEYBOARD;
    ip.ki.dwFlags = KEYEVENTF_UNICODE;
    ip.ki.wScan = 0x5B; // unicode value for '['

    Sleep(2000);

    SendInput(1, &ip, sizeof ip);

    return 0;
}

This will work regardless of keyboard layout but is technically a bit different then simulating a key press so using it depends on what exactly you are trying to do.

user2802841
  • 903
  • 7
  • 13