Is there a way to disable the Keyboard using C++? I need to keep the mouse active for user input.
If there is a way of doing it. Does anybody have an compiled file that will allow for this?
I appreciate the help.
:confused:
Printable View
Is there a way to disable the Keyboard using C++? I need to keep the mouse active for user input.
If there is a way of doing it. Does anybody have an compiled file that will allow for this?
I appreciate the help.
:confused:
I think the easiest way is to set a system-wide hook and just returning 0 in the Proc.. I know this is not sufficient to answer your question, but I'm at work and can't go into detail now.
Look up SetWindowsHookEx @ msdn and you might be able to work it out.
One warning, the proc needs to go into a DLL to be system-wide.. I'll post some examples later.. good luck!
edit:
Do you need it to be system-wide anyway? Or just from your app, since that's easier to accomplish!
Jop
I only need it to work while my app is running. So I don't think it really needs to be system wide.
Thanks
Well do you want it to block all keystrokes or just the ones that are being sent while your app is on the foreground?
Don't forget that you can NEVER block Ctrl+Alt+Del on the WinNT strain.
Sorry for the delay in getting back to you...
I would like to disable the keyboard while my app is running. Therefore blocking all keyboard input. But I would like to be able to reactivate the keyboard without having to reboot the machine.
If you could help I would appreciate it.
JOP:
I do not want the user to be able to use the keyboard at all while my application is running. But I do want them to have the option of using the mouse.
Hi turtle, I wrote you an example.. when the prog's running the keyboard is disabled :)
You need 2 projects, one Win32, and one DLL project.
The main app (main.cpp):
Note that it uses a dialog, you need to add it yourself (DLG_Main), or if you want the whole project zipped let me knowCode:#include <windows.h>
#include "resource.h"
//Global variables
typedef void (_stdcall *INITPROC)(HWND);
typedef VOID (*KILLPROC)(VOID);
INITPROC mInitProc;
KILLPROC mKillProc;
HWND g_hWnd;
HMODULE g_hDll;
BOOL CALLBACK MainDialogProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam){
switch(Message)
{
case WM_INITDIALOG:
g_hWnd = hwnd;
g_hDll = LoadLibrary("hook.dll");
mInitProc = (INITPROC) GetProcAddress(g_hDll, "InitHook");
if (mInitProc)
(mInitProc)(hwnd);
return TRUE;
case WM_CLOSE:
mKillProc = (KILLPROC) GetProcAddress(g_hDll, "KillHook");
if (mKillProc)
(mKillProc);
EndDialog(hwnd, WM_CLOSE);
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
g_hInst = hInstance;
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(DLG_Main), NULL, MainDialogProc);
return 0;
}
The DLL (hook.cpp):
The def file for the dll (hook.def):Code:#include <windows.h>
//CornedBee is my man for this part!!!
#pragma data_seg("shared_data")
#pragma bss_seg("shared_bss")
// All variables that should be available in all process spaces go here.
HHOOK g_hKeyHook;
HWND g_hWnd = NULL;
#pragma data_seg()
#pragma bss_seg()
#pragma comment(linker,"/SECTION:shared_data,RWS")
#pragma comment(linker,"/SECTION:shared_bss,RWS")
//Thanks CB!
LRESULT __stdcall CALLBACK KeyProc( int nCode, WPARAM wParam, LPARAM lParam ){
if (nCode < 0) return CallNextHookEx((HHOOK)g_hKeyHook, nCode, wParam, lParam);
return 1;
}
void __stdcall InitHook(HWND hWndOwner){
g_hWnd = hWndOwner;
g_hKeyHook = SetWindowsHookEx(WH_KEYBOARD, KeyProc,GetModuleHandle("hook.dll"),0);
if (!g_hKeyHook) MessageBox(NULL, "Hook is not working!!","MSG FROM DLL", MB_OK);
SetWindowText((HWND)g_hWnd, "Keyboard disabled!");
}
void __stdcall KillHook(){
UnhookWindowsHookEx(g_hKeyHook);
}
You need to add the def file to the dll project:Code:LIBRARY HOOK
EXPORTS
InitHook
KillHook
KeyProc
Project > Properties
then at
Linker > Input > Module Definition file: hook.def
I hope you can work it out, but again, if you need more help you're free to ask ;) enjoy!
Jop,
Many thanks to you for working on this problem for me. Is there any way that I could get you to finish writing the program and them posting the compiled file for me. I don't know C or C++, so that is why I am asking.
THANK YOU!
:)
One better and simpler way !
Use BlockInput function of Win32.
Sorry , but it will block Mouse tooo....
Yes it will, that's why I had to write this solution.
Turtle, why do I have to compile it? I mean don't you have a compiler? and what do you want to have than, a simple prog that just blocks the keyboard input? what are you going to use the prog for? Because it still will be possible to CTRL+ALT+DEL and close the app (In my case is visible so you can just close it using your mouse).
Do you want me to compile the dll only or the app itself too?
Someone please rewrite it in VB? I really need it for my next test. :)
#pragma data_seg()
#pragma bss_seg()
#pragma comment(linker,"/SECTION:shared_data,RWS")
#pragma comment(linker,"/SECTION:shared_bss,RWS")
It's not possible to rewrite that in VB.
Or, can you compile it, so I can use it as a library in VB ?
I found this thread when searching for how to set a system wide hook.
I got it to compile after adding: HINSTANCE g_hInst; to main.cpp, but when I run the program nothing happens. (No dialog is displayed). It might have to do with the .def file, because I never figured out how to add it to my linker settings... I found nowhere to specify a "Module Definition file"... How do I specify it? and is that the reason to why nothing happens when I run the app?
Just put the def file in your project, it alwasy sufficed for me.
The other thing: HINSTANCE is process-specific. System-wide hooks are special in that the code of them is run in various processes. This means that any HINSTANCE you access in a hook function might or might not have a meaning, or it might have a different meaning than you intend (0x00040000 usually refers to the main exe).
Thanks for the input CB
But I don't fully understand what you are trying to say... This is the first time I've written a hook. Should I declare the g_hInst somewhere else? Where?
You must not rely on the HINSTANCE. You must not write your hook so that it needs it.
Ohh, OK.
Thanks for the tip :)