Page 1 of 1

Really Usefull Header I made (Window's Key/Mouse Inputs)

Posted: Wed Apr 29, 2009 3:17 pm
by austech
Hello, Ive been working on a header class that makes using keyboard and mouse inputs easy. (WORKS ONLY ON WINDOWS, SORRY ;) )

Code: Select all

#include <Windows.h>
#pragma once

namespace Input
{

	namespace Key
	{
		void Hold_Shift()
		{
			keybd_event(VK_SHIFT,0,0,0);
		}
		void Undo_Shift()
		{
			keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0);
		}	
		
		void A()
		{
			keybd_event((0x41),0,0,0);
			keybd_event((0x41),0,KEYEVENTF_KEYUP,0);
		}			
		void B()
		{
			keybd_event((0x42),0,0,0);
			keybd_event((0x42),0,KEYEVENTF_KEYUP,0);
		}			
		void C()
		{
			keybd_event((0x43),0,0,0);
			keybd_event((0x43),0,KEYEVENTF_KEYUP,0);
		}				
		void D()
		{
			keybd_event((0x44),0,0,0);
			keybd_event((0x44),0,KEYEVENTF_KEYUP,0);
		}			
		void E()
		{
			keybd_event((0x45),0,0,0);
			keybd_event((0x45),0,KEYEVENTF_KEYUP,0);
		}			
		void F()
		{				
			keybd_event((0x46),0,0,0);
			keybd_event((0x46),0,KEYEVENTF_KEYUP,0);
		}			
		void G()
		{
			keybd_event((0x47),0,0,0);
			keybd_event((0x47),0,KEYEVENTF_KEYUP,0);
		}			
		void H()
		{
			keybd_event((0x48),0,0,0);
			keybd_event((0x48),0,KEYEVENTF_KEYUP,0);
		}				
		void I()
		{
			keybd_event((0x49),0,0,0);
			keybd_event((0x49),0,KEYEVENTF_KEYUP,0);
		}			
		void J()
		{
			keybd_event((0x4A),0,0,0);
			keybd_event((0x4A),0,KEYEVENTF_KEYUP,0);
		}			
		void K()
		{
			keybd_event((0x4B),0,0,0);
			keybd_event((0x4B),0,KEYEVENTF_KEYUP,0);
		}			
		void L()
		{
			keybd_event((0x4C),0,0,0);
			keybd_event((0x4C),0,KEYEVENTF_KEYUP,0);
		}			
		void M()
		{
			keybd_event((0x4D),0,0,0);
			keybd_event((0x4D),0,KEYEVENTF_KEYUP,0);
		}				
		void N()
		{
			keybd_event((0x4E),0,0,0);
			keybd_event((0x4E),0,KEYEVENTF_KEYUP,0);
		}			
		void O()
		{
			keybd_event((0x4F),0,0,0);
			keybd_event((0x4F),0,KEYEVENTF_KEYUP,0);
		}			
		void P()
		{
			keybd_event((0x50),0,0,0);
			keybd_event((0x50),0,KEYEVENTF_KEYUP,0);
		}			
		void Q()
		{
			keybd_event((0x51),0,0,0);
			keybd_event((0x51),0,KEYEVENTF_KEYUP,0);
		}			
		void R()
		{
			keybd_event((0x52),0,0,0);
			keybd_event((0x52),0,KEYEVENTF_KEYUP,0);
		}				
		void S()
		{
			keybd_event((0x53),0,0,0);
			keybd_event((0x53),0,KEYEVENTF_KEYUP,0);
		}			
		void T()
		{
			keybd_event((0x54),0,0,0);
			keybd_event((0x54),0,KEYEVENTF_KEYUP,0);
		}
		void U()
		{
			keybd_event((0x55),0,0,0);
			keybd_event((0x55),0,KEYEVENTF_KEYUP,0);
		}			
		void V()
		{
			keybd_event((0x56),0,0,0);
			keybd_event((0x56),0,KEYEVENTF_KEYUP,0);
		}			
		void W()
		{
			keybd_event((0x57),0,0,0);
			keybd_event((0x57),0,KEYEVENTF_KEYUP,0);
		}			
		void X()
		{	
			keybd_event((0x58),0,0,0);
			keybd_event((0x58),0,KEYEVENTF_KEYUP,0);
		}	
					
		void Y()
		{
			keybd_event((0x59),0,0,0);
			keybd_event((0x59),0,KEYEVENTF_KEYUP,0);
		}
		void Z()
		{
			keybd_event((0x5A),0,0,0);
			keybd_event((0x5A),0,KEYEVENTF_KEYUP,0);
		}	
		
		void Space()
		{
			keybd_event(VK_SPACE,0,0,0);
			keybd_event(VK_SPACE,0,KEYEVENTF_KEYUP,0);
		}

		void NumLock()
		{
			keybd_event(VK_NUMLOCK,0,0,0);
			keybd_event(VK_NUMLOCK,0,KEYEVENTF_KEYUP,0);
		}
		
		void Plus()
		{
			keybd_event(VK_ADD,0,0,0);
			keybd_event(VK_ADD,0,KEYEVENTF_KEYUP,0);
		}
		void Add()
		{
			keybd_event(VK_ADD,0,0,0);
			keybd_event(VK_ADD,0,KEYEVENTF_KEYUP,0);
		}

		void Minus()
		{
			keybd_event(VK_SUBTRACT,0,0,0);
			keybd_event(VK_SUBTRACT,0,KEYEVENTF_KEYUP,0);
		}
		void Subtract()
		{
			keybd_event(VK_SUBTRACT,0,0,0);
			keybd_event(VK_SUBTRACT,0,KEYEVENTF_KEYUP,0);
		}

		void Divide()
		{
			keybd_event(VK_DIVIDE,0,0,0);
			keybd_event(VK_DIVIDE,0,KEYEVENTF_KEYUP,0);
		}

		void F1()
		{
			keybd_event(VK_F1,0,0,0);
			keybd_event(VK_F1,0,KEYEVENTF_KEYUP,0);
		}
		void F2()
		{
			keybd_event(VK_F2,0,0,0);
			keybd_event(VK_F2,0,KEYEVENTF_KEYUP,0);
		}
		void F3()
		{
			keybd_event(VK_F3,0,0,0);
			keybd_event(VK_F3,0,KEYEVENTF_KEYUP,0);
		}
		void F4()
		{
			keybd_event(VK_F4,0,0,0);
			keybd_event(VK_F4,0,KEYEVENTF_KEYUP,0);
		}
		void F5()
		{
			keybd_event(VK_F5,0,0,0);
			keybd_event(VK_F5,0,KEYEVENTF_KEYUP,0);
		}
		void F6()
		{
			keybd_event(VK_F6,0,0,0);
			keybd_event(VK_F6,0,KEYEVENTF_KEYUP,0);
		}
		void F7()
		{
			keybd_event(VK_F7,0,0,0);
			keybd_event(VK_F7,0,KEYEVENTF_KEYUP,0);
		}
		void F8()
		{
			keybd_event(VK_F8,0,0,0);
			keybd_event(VK_F8,0,KEYEVENTF_KEYUP,0);
		}
		void F9()
		{
			keybd_event(VK_F9,0,0,0);
			keybd_event(VK_F9,0,KEYEVENTF_KEYUP,0);
		}
		void F10()
		{
			keybd_event(VK_F10,0,0,0);
			keybd_event(VK_F10,0,KEYEVENTF_KEYUP,0);
		}
		void F11()
		{
			keybd_event(VK_F11,0,0,0);
			keybd_event(VK_F11,0,KEYEVENTF_KEYUP,0);
		}
		void F12()
		{
			keybd_event(VK_F12,0,0,0);
			keybd_event(VK_F12,0,KEYEVENTF_KEYUP,0);
		}
		void Esc()
		{
			keybd_event(VK_ESCAPE,0,0,0);
			keybd_event(VK_ESCAPE,0,KEYEVENTF_KEYUP,0);
		}
	}

	namespace Mouse
	{
		void Left()
		{
			INPUT input[1] ;
			input[0].type = INPUT_MOUSE ;

			input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN ;
			input[0].mi.dwExtraInfo = 0 ;
			SendInput ( 1, input, sizeof ( INPUT ) ) ;

			input[0].mi.dwFlags = MOUSEEVENTF_LEFTUP ;
			SendInput ( 1, input, sizeof ( INPUT ) ) ; 
		}

		void Right()
		{
			INPUT input[1] ;
			input[0].type = INPUT_MOUSE ;

			input[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN ;
			input[0].mi.dwExtraInfo = 0 ;
			SendInput ( 1, input, sizeof ( INPUT ) ) ;

			input[0].mi.dwFlags = MOUSEEVENTF_RIGHTUP ;
			SendInput ( 1, input, sizeof ( INPUT ) ) ; 
		}

		void Hold_Left()
		{
			INPUT input[1] ;
			input[0].type = INPUT_MOUSE ;

			input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN ;
			input[0].mi.dwExtraInfo = 0 ;
			SendInput ( 1, input, sizeof ( INPUT ) ) ;
		}

		void Hold_Right()
		{
			INPUT input[1] ;
			input[0].type = INPUT_MOUSE ;

			input[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN ;
			input[0].mi.dwExtraInfo = 0 ;
			SendInput ( 1, input, sizeof ( INPUT ) ) ;
		}
	}

	namespace KeyIsDown
	{
		bool A()
		{
			if(GetAsyncKeyState((0x41)))
			return true;
			else
			return false;
		}
		bool B()
		{
			if(GetAsyncKeyState((0x42)))
			return true;
			else
			return false;
		}
		bool C()
		{
			if(GetAsyncKeyState((0x43)))
			return true;
			else
			return false;
		}
		bool D()
		{
			if(GetAsyncKeyState((0x44)))
			return true;
			else
			return false;
		}
		bool E()
		{
			if(GetAsyncKeyState((0x45)))
			return true;
			else
			return false;
		}
		bool F()
		{
			if(GetAsyncKeyState((0x46)))
			return true;
			else
			return false;
		}
		bool G()
		{
			if(GetAsyncKeyState((0x47)))
			return true;
			else
			return false;
		}
		bool H()
		{
			if(GetAsyncKeyState((0x48)))
			return true;
			else
			return false;
		}
		bool I()
		{
			if(GetAsyncKeyState((0x49)))
			return true;
			else
			return false;
		}
		bool J()
		{
			if(GetAsyncKeyState((0x4A)))
			return true;
			else
			return false;
		}
		bool K()
		{
			if(GetAsyncKeyState((0x4B)))
			return true;
			else
			return false;
		}
		bool L()
		{
			if(GetAsyncKeyState((0x4C)))
			return true;
			else
			return false;
		}
		bool M()
		{
			if(GetAsyncKeyState((0x4D)))
			return true;
			else
			return false;
		}
		bool N()
		{
			if(GetAsyncKeyState((0x4E)))
			return true;
			else
			return false;
		}
		bool O()
		{
			if(GetAsyncKeyState((0x4F)))
			return true;
			else
			return false;
		}
		bool P()
		{
			if(GetAsyncKeyState((0x50)))
			return true;
			else
			return false;
		}
		bool Q()
		{
			if(GetAsyncKeyState((0x51)))
			return true;
			else
			return false;
		}
		bool R()
		{
			if(GetAsyncKeyState((0x52)))
			return true;
			else
			return false;
		}
		bool S()
		{
			if(GetAsyncKeyState((0x53)))
			return true;
			else
			return false;
		}
		bool T()
		{
			if(GetAsyncKeyState((0x54)))
			return true;
			else
			return false;
		}
		bool U()
		{
			if(GetAsyncKeyState((0x55)))
			return true;
			else
			return false;
		}
		bool V()
		{
			if(GetAsyncKeyState((0x56)))
			return true;
			else
			return false;
		}
		bool W()
		{
			if(GetAsyncKeyState((0x57)))
			return true;
			else
			return false;
		}
		bool X()
		{
			if(GetAsyncKeyState((0x58)))
			return true;
			else
			return false;
		}
		bool Y()
		{
			if(GetAsyncKeyState((0x59)))
			return true;
			else
			return false;
		}
		bool Z()
		{
			if(GetAsyncKeyState((0x5A)))
			return true;
			else
			return false;
		}
	
		
		bool Plus()
		{
			if(GetAsyncKeyState(VK_ADD))
			return true;
			else
			return false;
		}
		bool Add()
		{
			if(GetAsyncKeyState(VK_ADD))
			return true;
			else
			return false;
		}

		bool Minus()
		{
			if(GetAsyncKeyState(VK_SUBTRACT))
			return true;
			else
			return false;
		}
		bool Subtract()
		{
			if(GetAsyncKeyState(VK_SUBTRACT))
			return true;
			else
			return false;
		}

		bool Divide()
		{
			if(GetAsyncKeyState(VK_DIVIDE))
			return true;
			else
			return false;
		}

		bool F1()
		{
			if(GetAsyncKeyState(VK_F1))
			return true;
			else
			return false;
		}
		bool F2()
		{
			if(GetAsyncKeyState(VK_F2))
			return true;
			else
			return false;
		}
		bool F3()
		{
			if(GetAsyncKeyState(VK_F3))
				return true;
			else
			return false;
		}
		bool F4()
		{
			if(GetAsyncKeyState(VK_F4))
				return true;
			else
			return false;
		}
		bool F5()
		{
			if(GetAsyncKeyState(VK_F5))
				return true;
			else
			return false;
		}
		bool F6()
		{
			if(GetAsyncKeyState(VK_F6))
				return true;
			else
			return false;
		}
		bool F7()
		{
			if(GetAsyncKeyState(VK_F7))
				return true;
			else
			return false;
		}
		bool F8()
		{
			if(GetAsyncKeyState(VK_F8))
				return true;
			else
			return false;
		}
		bool F9()
		{
			if(GetAsyncKeyState(VK_F9))
				return true;
			else
			return false;
		}
		bool F10()
		{
			if(GetAsyncKeyState(VK_F10))
				return true;
			else
			return false;
		}
		bool F11()
		{
			if(GetAsyncKeyState(VK_F11))
				return true;
			else
			return false;
		}
		bool F12()
		{
			if(GetAsyncKeyState(VK_F12))
				return true;
			else
			return false;
		}
		bool Esc()
		{
			if(GetAsyncKeyState(VK_ESCAPE))
				return true;
			else
			return false;
		}
	}


	namespace MouseIsDown
	{

		bool Left()
		{	
			if(GetAsyncKeyState(VK_LBUTTON))
				return true;
			else
			return false;
		}
		bool Right()
		{	
			if(GetAsyncKeyState(VK_RBUTTON))
				return true;
			else
			return false;
		}

	}
}
I didn't get every single key input due to lazyness :lol:

Here' some tutorials on how to use this sucker :

If you want to make your program to press the "a" key, you would type:

Input::Key::A();


if you want the program to do something if the "a" key was hit you would do this:

if(Input::KeyIsDown::A())
{
//DO SHIT
}

Same works for the mouse. Hope you find this use full.



*IMPORTANT*

You are free to use the header in your program, that's why I made it. Also you can add on to it which is why i pasted the whole source, (possibly gut the rest of the keys in there :lol: ) but please don't take full credit, i hate when people take all the credit of someone elses work...


Hope this comes in handy to some people :mrgreen:

Re: Realy Usefull Header I made

Posted: Wed Apr 29, 2009 6:06 pm
by MarauderIIC
Hmmm... there's definitely a lot of pattern, here. I'm sure it can be reduced somehow...

Re: Realy Usefull Header I made

Posted: Wed Apr 29, 2009 6:14 pm
by austech
MarauderIIC wrote:Hmmm... there's definitely a lot of pattern, here. I'm sure it can be reduced somehow...
How? Because it would be nice if it was reduced :S

Re: Realy Usefull Header I made

Posted: Wed Apr 29, 2009 6:30 pm
by MarauderIIC
Well, you do the same two function calls every time except for a parameter. So how about we pass that parameter into a general function? Let's see. The numbers you're passing are simply the character value. So how about

input.h

Code: Select all

#ifndef INPUT_H
#define INPUT_H
class Input {
public:
	void simulateKeypress(int key, bool release = true);
	bool keyIsDown(int key);
};
#endif
input.cpp

Code: Select all

#include <ctype> //for toupper
#include <windows.h>

using namespace std;

//pass any character or VK_ constant to this to simulate the keypress.
//used int because VK_ constants probably aren't valid characters
void Input::simulateKey(int key, bool release) {
	key = toupper(key);
	keybd_event(key,0,0,0);
	if (release)
		keybd_event(key, 0, KEYEVENTF_KEYUP, 0);
}

bool Input::keyIsDown(int key) {
	return GetAsyncKeyState(key);
}
That's keyboard. You can either do something similar for the mouse, or add an enumerator and a switch statement, or leave them as-is.

Re: Realy Usefull Header I made

Posted: Wed Apr 29, 2009 8:18 pm
by eatcomics
Well now we have 2 useful code snippets ;)

Re: Realy Usefull Header I made

Posted: Wed Apr 29, 2009 8:27 pm
by austech
eatcomics wrote:Well now we have 2 useful code snippets ;)
Thanks glad you liked it :mrgreen:

Re: Really Usefull Header I made (Window's Key/Mouse Inputs)

Posted: Tue May 05, 2009 5:32 pm
by Ewan
Looks pretty cool... Am I right in thinking you need a kind of 'game loop', or for instance if I put this with no loop or anything..

Code: Select all

if(Input::KeyIsDown::A())
{
//DO SHIT
}
Will it wait till the A key is pressed before proceeding?

Sorry if this is a dumb question :oops:

Re: Really Usefull Header I made (Window's Key/Mouse Inputs)

Posted: Tue May 05, 2009 5:37 pm
by eatcomics
Ewan wrote:Looks pretty cool... Am I right in thinking you need a kind of 'game loop', or for instance if I put this with no loop or anything..

Code: Select all

if(Input::KeyIsDown::A())
{
//DO SHIT
}
Will it wait till the A key is pressed before proceeding?

Sorry if this is a dumb question :oops:
No because it's an if statement, so it would see if A is hit do stuff, if not just skip it...

Re: Really Usefull Header I made (Window's Key/Mouse Inputs)

Posted: Tue May 05, 2009 6:38 pm
by Ewan
Good point.

I knew that. I was testing you... -shifty eyes-

Re: Really Usefull Header I made (Window's Key/Mouse Inputs)

Posted: Tue May 05, 2009 7:54 pm
by MarauderIIC
It looks like the code simulates a key press, but I'm not really sure, since I'm not familiar with what keybd_event does. Hence the name of my function *shrug*

Re: Really Usefull Header I made (Window's Key/Mouse Inputs)

Posted: Wed May 06, 2009 2:14 pm
by austech
This would need a loop unless you're really damn fast with hitting a key before the program ends :lol:

But here's a better example of how to use this with the good ol' hello world program :mrgreen:

Code: Select all

#include <iostream>
#include <Inputs.hpp>

int main()
{
	bool Run = true;

	while(Run)
	{
		if(Input::KeyIsDown::A())
		{
			std::cout<<"Hello world\n";
		}
	}
}


Is that easier to understand? Sorry for not showing a better example before D:


Also for MarauderIIC :

What keybd_event does it it pretty much simulates a key press. So instead of using my header and type Input::Key::A(); you could do

Code: Select all

keybd_event((0x41),0,0,0);

What that would do is press the A key, but I have this in my header:

Code: Select all

keybd_event((0x41),0,KEYEVENTF_KEYUP,0);
So that it will release the key, that way its not held down. As for the key checking, I use this to check if the A key is pressed:

Code: Select all

		bool A()
		{
			if(GetAsyncKeyState((0x41)))
			return true;
			else
			return false;
		}

I'm not %100 sure if this is exactly what it means, but this is what I think. GetAsyncKeyState checks if a key is pressed. So basicly in that code its saying if A is pressed return true. The 0x41 is the virtual key code. Theres a whole list of all the key codes on this link: http://msdn.microsoft.com/en-us/library/ms645540.aspx

Re: Really Usefull Header I made (Window's Key/Mouse Inputs)

Posted: Fri May 22, 2009 2:37 am
by RyanPridgeon
austech wrote:The 0x41 is the virtual key code. Theres a whole list of all the key codes on this link: http://msdn.microsoft.com/en-us/library/ms645540.aspx
This is just a simple hex version of an ASCII character value, so instead of making lots of seperate functions for each key, you could have 1 function where you pass the ASCII value like...

KeyIsDown(int value){
keybd_event((value),0,0,0);
keybd_event((value),0,KEYEVENTF_KEYUP,0);
}

and then use it like so

if(KeyIsDown('a')){
cout << "a is hit";
}

Re: Really Usefull Header I made (Window's Key/Mouse Inputs)

Posted: Fri May 22, 2009 10:51 am
by MarauderIIC
You'll see that that's what my version does, Ryan.