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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
austech
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 8
Joined: Fri Oct 31, 2008 10:53 pm

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

Post 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:
Last edited by austech on Sat May 02, 2009 4:58 pm, edited 2 times in total.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Realy Usefull Header I made

Post by MarauderIIC »

Hmmm... there's definitely a lot of pattern, here. I'm sure it can be reduced somehow...
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
austech
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 8
Joined: Fri Oct 31, 2008 10:53 pm

Re: Realy Usefull Header I made

Post 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
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Realy Usefull Header I made

Post 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.
Last edited by MarauderIIC on Wed Apr 29, 2009 6:36 pm, edited 4 times in total.
Reason: added keyIsDown
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Realy Usefull Header I made

Post by eatcomics »

Well now we have 2 useful code snippets ;)
Image
austech
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 8
Joined: Fri Oct 31, 2008 10:53 pm

Re: Realy Usefull Header I made

Post by austech »

eatcomics wrote:Well now we have 2 useful code snippets ;)
Thanks glad you liked it :mrgreen:
Ewan
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Mon Mar 23, 2009 11:46 am

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

Post 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:
They pull out the truncheon, that's when the trouble starts.

'Cause when you've got a badge, the laws don't apply.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

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

Post 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...
Image
Ewan
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Mon Mar 23, 2009 11:46 am

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

Post by Ewan »

Good point.

I knew that. I was testing you... -shifty eyes-
They pull out the truncheon, that's when the trouble starts.

'Cause when you've got a badge, the laws don't apply.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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*
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
austech
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 8
Joined: Fri Oct 31, 2008 10:53 pm

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

Post 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
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

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

Post 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";
}
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post by MarauderIIC »

You'll see that that's what my version does, Ryan.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply