Results 1 to 19 of 19

Thread: disable Only the Keyboard

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    Western North Carolina
    Posts
    124

    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.


  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    Western North Carolina
    Posts
    124
    I only need it to work while my app is running. So I don't think it really needs to be system wide.

    Thanks

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    Western North Carolina
    Posts
    124
    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.

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    Western North Carolina
    Posts
    124
    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!



  9. #9
    New Member krishnaa's Avatar
    Join Date
    Feb 2003
    Location
    India , Pune
    Posts
    10
    One better and simpler way !
    Use BlockInput function of Win32.
    Hi , Havent seen me before ???
    Not Even on Codeguru ?

  10. #10
    New Member krishnaa's Avatar
    Join Date
    Feb 2003
    Location
    India , Pune
    Posts
    10
    Sorry , but it will block Mouse tooo....
    Hi , Havent seen me before ???
    Not Even on Codeguru ?

  11. #11
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  12. #12
    New Member
    Join Date
    Aug 2002
    Location
    VietNam
    Posts
    12
    Someone please rewrite it in VB? I really need it for my next test.
    <<< ...::: Know more and more :::... >>>
    <<< ...::: Ask more and more :::... >>>

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    #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.

  14. #14
    New Member
    Join Date
    Aug 2002
    Location
    VietNam
    Posts
    12
    Or, can you compile it, so I can use it as a library in VB ?
    <<< ...::: Know more and more :::... >>>
    <<< ...::: Ask more and more :::... >>>

  15. #15
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  17. #17
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  19. #19
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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
  •  



Click Here to Expand Forum to Full Width