Disabling keys and system keys

Started by
1 comment, last by Vortez 10 years, 6 months ago

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.

Advertisement

Seem like im gonna need to use a hook... oh well rolleyes.gif

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;
	}
} 


This topic is closed to new replies.

Advertisement