Jump to content

Win32 Keyboard Shift Key Problem

- - - - -

  • Please log in to reply
2 replies to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I am trying to write a program that would wait for a Ctrl+7 keyboard shortcuts, and each time there is one, it should either set the VK_SHIFT key state to down, or to set it to up, depending on what it set it to last time the Ctrl+7 shortcut was used.

The problem, however, is that after two of those shortcuts, it won't do anything else, and the shift key state remains down, until I manually press and release the physical shift key.

I have tried both methods, using SendInput () and using keybd_event ().

SendInput () method:
#include <windows.h> 

#include <stdlib.h> 


int WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow){ 

	MSG msg; 

	struct INPUT input_data; 

	int i; 

	int state= 0; 

	memset (&input_data, 0, sizeof (input_data)); 

	RegisterHotKey (0, 1, MOD_CONTROL, 0x37); 

	while (GetMessage (&msg, 0, 0, 0)){ 

		if (msg.message == WM_HOTKEY){ 

			if (msg.wParam == 1){ 

				input_data.type= INPUT_KEYBOARD; 

				input_data.ki.wVk= VK_SHIFT; 

				input_data.ki.dwExtraInfo= GetMessageExtraInfo (); 

				if (state) input_data.ki.dwFlags= KEYEVENTF_KEYUP; 

				else input_data.ki.dwFlags= 0; 

				SendInput (1, &input_data, sizeof (INPUT)); 

				state= state * -1 + 1; 

			} 

		} else if (msg.message == WM_CLOSE){ 

			ReplyMessage (0); 

			break; 

		} else { 

			

		} 

		ReplyMessage (0); 

	} 

	UnregisterHotKey (0, 1); 

	ExitProcess (0); 

} 

keybd_event () method:
#include <windows.h> 

#include <stdlib.h> 


int WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow){ 

	MSG msg; 

	int state= 0; 

	RegisterHotKey (0, 1, MOD_CONTROL, 0x37); 

	while (GetMessage (&msg, 0, 0, 0)){ 

		if (msg.message == WM_HOTKEY){ 

			if (msg.wParam == 1){ 

				keybd_event (VK_SHIFT, 0, KEYEVENTF_KEYUP * state, GetMessageExtraInfo ()); 

				state= state * -1 + 1; 

			} 

		} else if (msg.message == WM_CLOSE){ 

			ReplyMessage (0); 

			break; 

		} else { 

			

		} 

		ReplyMessage (0); 

	} 

	UnregisterHotKey (0, 1); 

	ExitProcess (0); 

	return 0; 

} 


What is wrong with it? What's causing the unresponsiveness?


Thanks in advance.
RR

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Have you tried this code? It uses (apparently) deprecated keybd_event function.

Also, your state changing code can be optimized a bit:
state = 1 - state;
// or with logical not
state = !state;

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Okay, I tried it, but still the same result.

By the way, for some reason the calls to printf () don't work either. I usually do some sort of output when I debug things, but at times when the output function doesn't work, it usually either means that there's something wrong with the call to the output function, or it means that the code that calls the output function is not executed at all - which doesn't make sense, because the code does run for a couple of Ctrl+7 s, before it gets unresponsive until the next physical shift key press.

Here's some updated code:
#include <windows.h> 

#include <stdlib.h> 

#include <stdio.h> 


int WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow){ 

	MSG msg; 

	int state= 0; 

	RegisterHotKey (0, 1, MOD_CONTROL, 0x37); 

	while (GetMessage (&msg, 0, 0, 0)){ 

		if (msg.message == WM_HOTKEY){ 

			if (msg.wParam == 1){ 

				printf ("stage 1\r\n"); 

				keybd_event (VK_LSHIFT, 0, KEYEVENTF_KEYUP * state + KEYEVENTF_EXTENDEDKEY, 0 * (int)(GetMessageExtraInfo ())); 

				printf ("stage 2\r\n"); 

				state= 1 - state; 

			} 

		} else if (msg.message == WM_CLOSE){ 

			ReplyMessage (0); 

			break; 

		} else { 

			

		} 

		ReplyMessage (0); 

	} 

	UnregisterHotKey (0, 1); 

	ExitProcess (0); 

	return 0; 

} 





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users