Domanda

I am trying to implement a SendKeys() function with SendInput(), and here is my key snippet of my function:

void static SendKeys(string s){
int modifier=0x00;
string s1=s.substr(0, 1);
int key=0x00;

if (s1=="+"){
    s.erase(0, 1);
    modifier=VK_SHIFT;
}
else if ( s1=="^"){
    s.erase(0, 1);
    modifier=VK_CONTROL;
}
else if (s1=="%"){
    s.erase(0, 1);
    modifier=VK_MENU;
}
else if (s1=="!"){
    s.erase(0, 1);
    modifier=VK_LWIN;
}

...

if(s=="A"||s=="a"){
    key=0x41;
}
else if(s=="B"||s=="b"){

...

else if(s=="Z"||s=="z"){
key=0x5A;
}
INPUT input;
INPUT inputArray[4];
input.type=INPUT_KEYBOARD;

input.ki.dwFlags=0;
input.ki.wVk = modifier;
input.ki.wScan = MapVirtualKey(modifier, MAPVK_VK_TO_VSC);
inputArray[0] = input;

input.ki.dwFlags = 0;
input.ki.wVk = key;
input.ki.wScan = MapVirtualKey(key, MAPVK_VK_TO_VSC);
inputArray[1] = input;

input.ki.wVk = key;
input.ki.wScan = MapVirtualKey(key, MAPVK_VK_TO_VSC);
input.ki.dwFlags = KEYEVENTF_KEYUP;
inputArray[2] = input;

input.ki.wVk = key;
input.ki.wScan = MapVirtualKey(modifier, MAPVK_VK_TO_VSC);
input.ki.dwFlags = KEYEVENTF_KEYUP;
inputArray[3] = input;

SendInput(4,inputArray,sizeof(INPUT));
}

And here is my test code:

int main(int argc, char *argv[])
{
while(true){
    Sleep(1000);
    SendKeys("+h");
    SendKeys("e");
    SendKeys("l");
    SendKeys("l");
    SendKeys("o");
    Sleep(1000);
}
return 0;
}

Instead "Hello", I keep receiving "HELLO", and after the program was terminated, the ctrl button seems is still being pressed. Wondering what happened.

È stato utile?

Soluzione

I think your problem lied in here

input.ki.wVk = key; // <-- should be modifier
input.ki.wScan = MapVirtualKey(modifier, MAPVK_VK_TO_VSC);
input.ki.dwFlags = KEYEVENTF_KEYUP;
inputArray[3] = input;

Therefore your program didn't release the SHIFT key.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top