|
-
Jan 28th, 2003, 08:53 AM
#1
Thread Starter
Lively Member
disable Only the Keyboard
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.
-
Jan 28th, 2003, 09:37 AM
#2
Frenzied Member
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
Last edited by Jop; Jan 28th, 2003 at 09:43 AM.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 28th, 2003, 07:19 PM
#3
Thread Starter
Lively Member
I only need it to work while my app is running. So I don't think it really needs to be system wide.
Thanks
-
Jan 28th, 2003, 07:53 PM
#4
Frenzied Member
Well do you want it to block all keystrokes or just the ones that are being sent while your app is on the foreground?
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 28th, 2003, 08:35 PM
#5
Don't forget that you can NEVER block Ctrl+Alt+Del on the WinNT strain.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 3rd, 2003, 09:25 AM
#6
Thread Starter
Lively Member
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.
-
Feb 3rd, 2003, 11:09 AM
#7
Frenzied Member
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):
Code:
#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;
}
Note that it uses a dialog, you need to add it yourself (DLG_Main), or if you want the whole project zipped let me know
The DLL (hook.cpp):
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);
}
The def file for the dll (hook.def):
Code:
LIBRARY HOOK
EXPORTS
InitHook
KillHook
KeyProc
You need to add the def file to the dll project:
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!
Last edited by Jop; Feb 3rd, 2003 at 12:26 PM.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Feb 4th, 2003, 09:03 AM
#8
Thread Starter
Lively Member
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!
-
Feb 5th, 2003, 08:04 AM
#9
New Member
One better and simpler way !
Use BlockInput function of Win32.
-
Feb 5th, 2003, 08:11 AM
#10
New Member
Sorry , but it will block Mouse tooo....
-
Feb 5th, 2003, 09:41 AM
#11
Frenzied Member
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?
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Apr 27th, 2003, 06:42 AM
#12
New Member
Someone please rewrite it in VB? I really need it for my next test.
<<< ...::: Know more and more :::... >>>
<<< ...::: Ask more and more :::... >>>
-
Apr 27th, 2003, 07:42 AM
#13
#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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 28th, 2003, 12:31 AM
#14
New Member
Or, can you compile it, so I can use it as a library in VB ?
<<< ...::: Know more and more :::... >>>
<<< ...::: Ask more and more :::... >>>
-
Aug 18th, 2003, 08:09 AM
#15
Fanatic Member
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?
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Aug 20th, 2003, 01:39 AM
#16
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).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Aug 20th, 2003, 07:43 AM
#17
Fanatic Member
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?
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Aug 21st, 2003, 01:25 AM
#18
You must not rely on the HINSTANCE. You must not write your hook so that it needs it.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Aug 21st, 2003, 02:47 AM
#19
Fanatic Member
Ohh, OK.
Thanks for the tip
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|