Jump to content
  • Advertisement
Sign in to follow this  
Vortez

Disabling keys and system keys

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im building a remote desktop application, everything work fine now except for one thing...

 

Let's say i press the windows key, the start menu pop up in the controled machine as expected, but also pop up on the machine that's controling the other and the same is true for stuff like alt-space ect.

 

As a side note, Im using c++ builder for my project.

 

Now, i think i could maybe get rid of this by using a keyboard hook, but id really like to avoid this as much as possible.

 

So far, i tried this

class TViewerForm : public TForm
{
...
protected:
    virtual void __fastcall WndProc(Messages::TMessage &Message);
...
}


void __fastcall TViewerForm::WndProc(Messages::TMessage &Message)
{
	if(Message.Msg == WM_KEYDOWN || Message.Msg == WM_KEYUP || Message.Msg == WM_SYSKEYDOWN || Message.Msg == WM_SYSKEYUP){
		return;
	}

	TForm::WndProc(Message); // Default processing for any other message
}

but it dosen't seem to work.

 

If anybody as an idea plz let me know.

 

Share this post


Link to post
Share on other sites
Advertisement

Got it to work using this monstruosity laugh.png

#include "KbHook.h"

//the following is to allow the sharing of data between instances of this dll...
#pragma data_seg(".SHARDAT")
static HINSTANCE hInstance = NULL;
static HHOOK hHook = NULL;
static HWND hViewerWnd = NULL;
static void *OnKeyEvent = NULL;
#pragma data_seg()


///////////////////////////////////////////////////////////////////////////
// Dll Entry point
///////////////////////////////////////////////////////////////////////////
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD fdwreason,  LPVOID lpReserved)
{
    switch(fdwreason)
	{
    case DLL_PROCESS_ATTACH:
		hInstance = hInst;
		break;
    case DLL_THREAD_ATTACH:  break;
    case DLL_THREAD_DETACH:  break;
    case DLL_PROCESS_DETACH: break;
    }
    return TRUE;
}

///////////////////////////////////////////////////////////////////////////
// The keyboard hook callback
///////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
	if(code >= 0){

		// Get the cursor position
		POINT p;
		GetCursorPos(&p);

		// Check if the window under the cursor is the renderer window
		if(hViewerWnd == WindowFromPoint(p)){
		
			// If so, call the external function OnKeyEvent(DWORD wParam, DWORD lParam)
			if(OnKeyEvent != NULL){
				_asm {
					push lParam
					push wParam			

					call OnKeyEvent
					add esp, 8 
				}
			}

			// ... and disable the keyboard
			return 1;
		}
	}

	// Process key normally
	return CallNextHookEx(hHook, code, wParam, lParam);
}

///////////////////////////////////////////////////////////////////////////
// Install the hook...
///////////////////////////////////////////////////////////////////////////
BOOL WINAPI _InstallHook(HWND ViewerWnd, void *pKeyEventFunc)
{
	if(!hHook){

		hViewerWnd = ViewerWnd;
		OnKeyEvent = pKeyEventFunc;

		hHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyboardProc, hInstance, 0);
	}

	return hHook != NULL;
}

///////////////////////////////////////////////////////////////////////////
// Remove the hook...
///////////////////////////////////////////////////////////////////////////
void WINAPI _RemoveHook()
{
	if(hHook){

		UnhookWindowsHookEx(hHook);

		hHook = NULL;
		hViewerWnd = NULL;
		OnKeyEvent = NULL;
	}
} 


Edited by Vortez

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!