Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » GET KEY STATE for Linux and win32
GET KEY STATE for Linux and win32 [message #23240] Fri, 02 October 2009 11:49 Go to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Hello All!

I was implemented an interesting method for get key state:
The code like this:
bool get_key_state(dword p_k){
#ifdef PLATFORM_WIN32
		//... Here need to optimize
		if(p_k & 0xffff)
			return (((char)::GetKeyState(p_k & 0xffff)) & 128);
		
		if(p_k & K_ALT)
			return GetAlt();
			
		if(p_k & K_SHIFT)
			return GetShift();
			
		if(p_k & K_CTRL)
			return GetCtrl();
#elif PLATFORM_X11
		char key_map_stat[32];
		XQueryKeymap(Xdisplay, key_map_stat);
		
		if(p_k & 0xffff){
			KeyCode k_code = XKeysymToKeycode(Xdisplay, p_k & 0xffff);
			return ((key_map_stat[k_code >> 3] >> (k_code & 7)) & 1);
		}
		
		if(p_k & K_ALT){
			const KeyCode alt_l = XKeysymToKeycode(Xdisplay,XK_Alt_L);
			const KeyCode alt_r = XKeysymToKeycode(Xdisplay,XK_Alt_R);
			
			return (((key_map_stat[alt_l >> 3] >> (alt_l & 7)) & 1)
				  ||((key_map_stat[alt_r >> 3] >> (alt_r & 7)) & 1));
		}
			
		if(p_k & K_SHIFT){
			const KeyCode shift_l = XKeysymToKeycode(Xdisplay,XK_Shift_L);
			const KeyCode shift_r = XKeysymToKeycode(Xdisplay,XK_Shift_R);
			
			return (((key_map_stat[shift_l >> 3] >> (shift_l & 7)) & 1)
				  ||((key_map_stat[shift_r >> 3] >> (shift_r & 7)) & 1));
		}
		
		if(p_k & K_CTRL){
			const KeyCode control_l = XKeysymToKeycode(Xdisplay,XK_Control_L);
			const KeyCode control_r = XKeysymToKeycode(Xdisplay,XK_Control_R);
			
			return (((key_map_stat[control_l >> 3] >> (control_l & 7)) & 1)
				  ||((key_map_stat[control_r >> 3] >> (control_r & 7)) & 1));
		}
#else
		//ASSERT_(false, "Get key state is not implemented for this plathform")
#endif
		return false;
}


It is first variant for get key state.
This method is missing in U++.
Any comments are welcome.
Re: GET KEY STATE for Linux and win32 [message #23241 is a reply to message #23240] Fri, 02 October 2009 12:40 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
tojocky wrote on Fri, 02 October 2009 05:49

Hello All!

I was implemented an interesting method for get key state:
The code like this:
bool get_key_state(dword p_k){
#ifdef PLATFORM_WIN32
		//... Here need to optimize
		if(p_k & 0xffff)
			return (((char)::GetKeyState(p_k & 0xffff)) & 128);
		
		if(p_k & K_ALT)
			return GetAlt();
			
		if(p_k & K_SHIFT)
			return GetShift();
			
		if(p_k & K_CTRL)
			return GetCtrl();
#elif PLATFORM_X11
		char key_map_stat[32];
		XQueryKeymap(Xdisplay, key_map_stat);
		
		if(p_k & 0xffff){
			KeyCode k_code = XKeysymToKeycode(Xdisplay, p_k & 0xffff);
			return ((key_map_stat[k_code >> 3] >> (k_code & 7)) & 1);
		}
		
		if(p_k & K_ALT){
			const KeyCode alt_l = XKeysymToKeycode(Xdisplay,XK_Alt_L);
			const KeyCode alt_r = XKeysymToKeycode(Xdisplay,XK_Alt_R);
			
			return (((key_map_stat[alt_l >> 3] >> (alt_l & 7)) & 1)
				  ||((key_map_stat[alt_r >> 3] >> (alt_r & 7)) & 1));
		}
			
		if(p_k & K_SHIFT){
			const KeyCode shift_l = XKeysymToKeycode(Xdisplay,XK_Shift_L);
			const KeyCode shift_r = XKeysymToKeycode(Xdisplay,XK_Shift_R);
			
			return (((key_map_stat[shift_l >> 3] >> (shift_l & 7)) & 1)
				  ||((key_map_stat[shift_r >> 3] >> (shift_r & 7)) & 1));
		}
		
		if(p_k & K_CTRL){
			const KeyCode control_l = XKeysymToKeycode(Xdisplay,XK_Control_L);
			const KeyCode control_r = XKeysymToKeycode(Xdisplay,XK_Control_R);
			
			return (((key_map_stat[control_l >> 3] >> (control_l & 7)) & 1)
				  ||((key_map_stat[control_r >> 3] >> (control_r & 7)) & 1));
		}
#else
		//ASSERT_(false, "Get key state is not implemented for this plathform")
#endif
		return false;
}


It is first variant for get key state.
This method is missing in U++.
Any comments are welcome.


What is wrong with

bool GetShift();
bool GetCtrl();
bool GetAlt();
bool GetCapsLock();
bool GetMouseLeft();
bool GetMouseRight();
bool GetMouseMiddle();

?

(But a nice find anyway...)

Mirek

[Updated on: Fri, 02 October 2009 12:41]

Report message to a moderator

Re: GET KEY STATE for Linux and win32 [message #23242 is a reply to message #23241] Fri, 02 October 2009 12:42 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Ah, sorry, now I see the point.

Mirek
Re: GET KEY STATE for Linux and win32 [message #23243 is a reply to message #23242] Fri, 02 October 2009 13:11 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

The second variant of code is:
bool get_key_state(dword p_k){
	bool result;
	#ifdef PLATFORM_WIN32
		result = true;
		//... Here need to optimize
		if(p_k & 0xffff)
			result &= (bool)(((char)::GetKeyState(p_k & 0xffff)) & 128);
		
		if((result)&&(p_k & K_ALT))
			result &= GetAlt();
			
		if((result)&&(p_k & K_SHIFT))
			result &= GetShift();
			
		if((result)&&(p_k & K_CTRL))
			result &= GetCtrl();
	#elif PLATFORM_X11
		result = true;
		char key_map_stat[32];
		XQueryKeymap(Xdisplay, key_map_stat);
		
		if(p_k & 0xffff){
			KeyCode k_code = XKeysymToKeycode(Xdisplay, p_k & 0xffff);
			result &= ((key_map_stat[k_code >> 3] >> (k_code & 7)) & 1);
		}
		
		if((result)&&(p_k & K_ALT)){
			const KeyCode alt_l = XKeysymToKeycode(Xdisplay,XK_Alt_L);
			const KeyCode alt_r = XKeysymToKeycode(Xdisplay,XK_Alt_R);
			
			result &= (((key_map_stat[alt_l >> 3] >> (alt_l & 7)) & 1)
				  ||((key_map_stat[alt_r >> 3] >> (alt_r & 7)) & 1));
		}
			
		if((result)&&(p_k & K_SHIFT)){
			const KeyCode shift_l = XKeysymToKeycode(Xdisplay,XK_Shift_L);
			const KeyCode shift_r = XKeysymToKeycode(Xdisplay,XK_Shift_R);
			
			result &= (((key_map_stat[shift_l >> 3] >> (shift_l & 7)) & 1)
				  ||((key_map_stat[shift_r >> 3] >> (shift_r & 7)) & 1));
		}
		
		if((result)&&(p_k & K_CTRL)){
			const KeyCode control_l = XKeysymToKeycode(Xdisplay,XK_Control_L);
			const KeyCode control_r = XKeysymToKeycode(Xdisplay,XK_Control_R);
			
			result &= (((key_map_stat[control_l >> 3] >> (control_l & 7)) & 1)
				  ||((key_map_stat[control_r >> 3] >> (control_r & 7)) & 1));
		}
	#else
		result = false;
		//ASSERT_(false, "Get key state is not implemented for this platform")
	#endif
		return result;
}


This will process correct:
if(get_key_state(K_CTRL_C)){
..................// add our code
}


Mirek, is it useful to add in U++?

[Updated on: Fri, 02 October 2009 13:16]

Report message to a moderator

Re: GET KEY STATE for Linux and win32 [message #24554 is a reply to message #23243] Sun, 24 January 2010 10:51 Go to previous message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Hello Mirek,

I have not feedback about my propose!


tojocky wrote on Fri, 02 October 2009 14:11

The second variant of code is:
bool get_key_state(dword p_k){
	bool result;
	#ifdef PLATFORM_WIN32
		result = true;
		//... Here need to optimize
		if(p_k & 0xffff)
			result &= (bool)(((char)::GetKeyState(p_k & 0xffff)) & 128);
		
		if((result)&&(p_k & K_ALT))
			result &= GetAlt();
			
		if((result)&&(p_k & K_SHIFT))
			result &= GetShift();
			
		if((result)&&(p_k & K_CTRL))
			result &= GetCtrl();
	#elif PLATFORM_X11
		result = true;
		char key_map_stat[32];
		XQueryKeymap(Xdisplay, key_map_stat);
		
		if(p_k & 0xffff){
			KeyCode k_code = XKeysymToKeycode(Xdisplay, p_k & 0xffff);
			result &= ((key_map_stat[k_code >> 3] >> (k_code & 7)) & 1);
		}
		
		if((result)&&(p_k & K_ALT)){
			const KeyCode alt_l = XKeysymToKeycode(Xdisplay,XK_Alt_L);
			const KeyCode alt_r = XKeysymToKeycode(Xdisplay,XK_Alt_R);
			
			result &= (((key_map_stat[alt_l >> 3] >> (alt_l & 7)) & 1)
				  ||((key_map_stat[alt_r >> 3] >> (alt_r & 7)) & 1));
		}
			
		if((result)&&(p_k & K_SHIFT)){
			const KeyCode shift_l = XKeysymToKeycode(Xdisplay,XK_Shift_L);
			const KeyCode shift_r = XKeysymToKeycode(Xdisplay,XK_Shift_R);
			
			result &= (((key_map_stat[shift_l >> 3] >> (shift_l & 7)) & 1)
				  ||((key_map_stat[shift_r >> 3] >> (shift_r & 7)) & 1));
		}
		
		if((result)&&(p_k & K_CTRL)){
			const KeyCode control_l = XKeysymToKeycode(Xdisplay,XK_Control_L);
			const KeyCode control_r = XKeysymToKeycode(Xdisplay,XK_Control_R);
			
			result &= (((key_map_stat[control_l >> 3] >> (control_l & 7)) & 1)
				  ||((key_map_stat[control_r >> 3] >> (control_r & 7)) & 1));
		}
	#else
		result = false;
		//ASSERT_(false, "Get key state is not implemented for this platform")
	#endif
		return result;
}


This will process correct:
if(get_key_state(K_CTRL_C)){
..................// add our code
}


Mirek, is it useful to add in U++?

Previous Topic: Polymorphic XMLizer
Next Topic: Not show baloon message in TrayIcon!
Goto Forum:
  


Current Time: Mon Apr 29 10:42:09 CEST 2024

Total time taken to generate the page: 0.03942 seconds