lundi 27 juin 2016

How would it be possible to perfectly simulate sent input as keyboard input?


A little bit of background knowledge. I have lately been working on my own project to gain a little insight of sending inputs via program to move as one would with a keyboard/mouse. I decided to target a game and begin on the process of sending inputs. Which initially ran me into many issues such as the normal values for buttons in the games being switched around with random values. Some send inputs which were supposed to work not having any effect on the game. All of this however was solved but the obvious issue was still present.

Sending input was not smooth and tended to bug out a lot. For example I would send the z key and release it and this would work fine at times but on other times it would simply simulate a hold event in the game (even though it has been released) and I had started to come across weird changes that made this a lot less buggy , for example using a while loop as oppose to a for loop and checking whether the action has occurred before sending the next one.

As examples speak better than words:

int F(INPUT kb, int z) {
kb.ki.dwFlags = KEYEVENTF_SCANCODE;
kb.ki.time = 1;
kb.ki.wVk = 0;
kb.ki.wScan = z;
while (kb.ki.dwFlags != KEYEVENTF_SCANCODE) {
    kb.ki.dwFlags = KEYEVENTF_SCANCODE;
}
SendInput(1, &kb, sizeof(INPUT));

Sleep(110);
while( kb.ki.dwFlags == KEYEVENTF_SCANCODE){ kb.ki.dwFlags = KEYEVENTF_KEYUP; }


SendInput(1, &kb, sizeof(INPUT));
return 0;
}

This is pretty much the generic structure I use for sending a button and releasing it at first I was attempting to loop across this with a while loop which succeeds 96% of the time, where the other 4% it starts to hold the button and a for loop has near no success. The issue starts getting more complicated when I decide to use multiple buttons as then the success rate is near 0%.

I was attempting to make a smooth and efficient version however I do want to get the basic outline first so excuse any inefficiency..

Combining any inputs at the moment looks something like this:

int W(INPUT kb, int z,int right,int up) {

while(1){
    Sleep(100);
    F(kb, z);
    Sleep(100);
    R(kb,right);

}

return 0; }

I am attempting to make this more smooth with holding keys letting go and pressing multiple at a time. I appreciate any insight on this issue and any tips.


Aucun commentaire:

Enregistrer un commentaire