Using gamepad controller to send 'fake' keyboard inputs
17 replies, posted
Hey there guys! im sorry if you've seen this alot, but i can't seem to find the information i need to get this to work.
So i recently bought a xbox wired controller for my pc. My plan is to create my own program using C++ to use the controller in games that doesn't support controllers.
Yeah i know there are softwares already done for this, but i want to customise it more than just applying different keystrokes to the controller.
The game im focusing right now is ( league of legends )
So the thing i need my program to do is to take the controller signals, transfer them into fake keyboard inputs.
Hey why don't you use sendinput postmessage (or whatever there is) etc.. Because it doesn't work the way i want. The application probably has a "botting preventing system" seeing the difference between the keyboard signals, and the ones i create.
Something i found work was using sendinput() in the chat of the game. doesn't work in the game tho. If you get what i mean...
I've heard about hooking, don't know much about it. But is that the way to go, and is it possible? ( Does it get very advanced?)
(I'll use Xinput for the controller inputs)
You definitely can send inputs via the SendInput function on windows, or if you feel like being deprecated you can use keybd_event
If it doesnt work in a game, its likely because you aren't sending both a down then up event
There is no distinction between real keyboard input and sendinput (I have successfully used this to play various games with a leap motion)
Make sure you aren't spamming keydown every frame. You need to send a keydown then a keyup, then stop sending input. I'm not sure if they can immediately follow each other or if it needs to be across a frame tiem (probably the former).
Keys are down until a keyup is sent I think, so make sure you aren't just spamming keydown because that probably wont work
Thank you for the quick response!
I'm sitting here working on it right now and have managed to use the controller thumbstick to
move the mouse from the center + thumbstick Position.
I'll check out what you've told me!
cheers!
[video=youtube;YQ7eFIn2V-c]https://www.youtube.com/watch?v=YQ7eFIn2V-c&feature=youtu.be[/video]
Im pretty bad at programming but here is what i've done so far, i just took the xinput sample and inserted some code :P
Hey im back!
It actually worked! :o
im using sendinput() and it actually seems to work pretty well! there has to be a time frame. Still experimenting...
im now binding all the keys to the controller.
If you're doing this as a learning exercise, keep it up.
But... you're not actually gonna play LoL with a controller, are you?
Haha yeah i have to try :P
Here is a vid of what i've done so far. I pretty much did this the whole day X)
There was about 180 lines of code! :)
[video=youtube;FW86_60pQv4]https://www.youtube.com/watch?v=FW86_60pQv4&feature=youtu.be[/video]
[QUOTE=j0nte33;45682585]Haha yeah i have to try :P
Here is a vid of what i've done so far. I pretty much did this the whole day X)
There was about 180 lines of code! :)
[video=youtube;FW86_60pQv4]https://www.youtube.com/watch?v=FW86_60pQv4&feature=youtu.be[/video][/QUOTE]
The video tags are a bit different here, you'd have to use [quote][noparse][video]http://www.youtube.com/watch?v=FW86_60pQv4[/video][/noparse][/quote] like so: [video=youtube;FW86_60pQv4]http://www.youtube.com/watch?v=FW86_60pQv4[/video]
Note that it's http without the s, I don't think Garry will fix it any time soon.
Ahh thanks!
check out the new vid i made! :D i played a normal game and recorded it!
[video=youtube;bWwz_H_IoG4]http://www.youtube.com/watch?v=bWwz_H_IoG4[/video]
I have to much free time! :-]
Hey guys!
As you can see, the program worked out great!:dance:
I know the code i made isn't optimal and i need you guys to help me.
Right now im updating the program with Sleep(); functions.
i use them after the game loop, and when i send keypress.
I've heard it's not optimal, and i need to know what would be a better way?
I did this exact thing a few months ago; using the xbox 360 remote to control the PC.
The best way I found was to use [url=http://inputsimulator.codeplex.com/]windows input simulator[/url] and [url=http://slimdx.org/]SlimDX[/url] XInput to get the controller state. It was much easier than using sendinput windows function, which sometimes caused a few problems not registering calls in some cercumstances.
Using those two libraries it was just 30 mins work wrapping up all the input states and making a GUI with all the keyboard key codes (inc media and special cases).
That's amazing man! :)
I have to check that out!
Tho, i don't understand how SlimDX will come in hand. Xinput is super easy to set up and use :P
It was what I had at the time on my laptop which has an [url=http://slimdx.org/docs/html/N_SlimDX_XInput.htm]XInput library[/url]. It was only a quick project so I could hook it up to a TV and watch videos from. Occasionally played Warhammer Dawn of War with it too.
Okay! ;P
newb question incoming, how do i "include" windows input simulator? T_T
I've never used anything like this before...
I made an update to the program wich takes information from a settings.txt file.
This means the program is now all of a sudden pretty customisable. here is an example from settings.txt:
[CODE]
rightTrigger=button(21)
rightButton=mouse(1,0)(held)
A=button(11)
RIGHT=button(0A)
STICK=centerMouse // the sticks will controller the mouse from the center of the screen
MODE //End of mode ( modes lets you switch through different sets of bindings)
[/CODE]
im thinking of adding more stuff as times goes on.
Currently at 700 lines! :tinfoil::pwn:
Looks great ! But it will require you to learn from scratch to play with the controller, as the first day playing lol with keyboard. Keep it up !
Odd... I just started basically the same project. Did you also see the video of the guy doing this with generic joystick to mouse software and think "I can make a better program than that" haha?
Can you go into a little more detail as to how you got your keyboard commands and mouse inputs to register to register?
I'm using the SendInput function and while it works well in windows (like I can click on things in the desktop and it will print QWER into notepad) I actually use the mouse to move my champion or press buttons to cast their abilities.
Hey! I got interessted watching a video like this [url]https://www.youtube.com/watch?v=opK9EPv_czM[/url].
Not that one but a similar, and i thought yeah, i can make it work better!!!
so i just made the program intended to work better for l.o.l.
for the mouse i used the function SetPhysicalCursorPos();
I'm actually not 100% sure this is the right way to use since there seems to be other similar function. but it works!
here is the code i send wich sends keypress: (i found this on internet :tinfoil:)
[CODE]
void pressButton(int key,int time)
{
//Set up the INPUT structure
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.wVk = 0; //We're doing scan codes instead
ip.ki.dwExtraInfo = 0;
//This let's you do a hardware scan instead of a virtual keypress
ip.ki.dwFlags = KEYEVENTF_SCANCODE;
ip.ki.wScan = (WORD)key; //Set a unicode character to use (A)
//Send the press
SendInput(1, &ip, sizeof(INPUT));
Sleep(time);
//Prepare a keyup event
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}[/CODE]
As you can see, i use sleep. Wich works for me. Probably not the best to use S: i found sleep(50) works well
Very nice that definitely was the problem. I actually just use the press/release of the buttons to do this. So if you like to use quickcast you get the cast on the negative edge just like you do with the keyboard. Also no sleep :) The downside is I have to keep track of the previous state of the key...
Anyway here's my code.
[CODE]
static void SetKeyDown(WORD key, INPUT& input)
{
input.type = INPUT_KEYBOARD;
input.ki.wVk = NULL;
input.ki.time = NULL;
input.ki.wScan = key;
input.ki.dwFlags = KEYEVENTF_SCANCODE;
input.ki.dwExtraInfo = NULL;
}
static void SetKeyUp(WORD key, INPUT& input)
{
input.type = INPUT_KEYBOARD;
input.ki.wVk = NULL;
input.ki.time = NULL;
input.ki.wScan = key;
input.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
input.ki.dwExtraInfo = NULL;
}
static void HandeKey(bool value, bool& previousValue, INPUT& input, UINT& nInputs, WORD key)
{
// If no change, do nothing
if(value == previousValue)
{
return;
}
if(value)
{
SetKeyDown(key, input);
}
else
{
SetKeyUp(key, input);
}
previousValue = value;
++nInputs;
}
[/CODE]
I'm still not getting mouse events clicks though... did you just bind the click functions to left click to a set of keys?
Sorry, you need to Log In to post a reply to this thread.