Results 1 to 7 of 7

Thread: Diable keyboard and mouse

  1. #1
    Con
    Guest

    Smile

    Hi, guys....I am new here and need some help.
    I am trying to create a program that will disable the keyboard and mouse and then enable them back.
    My program will run under windows 95, so the API BlockInput will not work. And, my program should be able to disable the keyboard and mouse even when my program is not on focus (because my program will call other application and simulate some keystrokes, which will cause other applications to gain focus).
    I had tried the following code, but it is not working. Something's wrong...?

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Private Sub Form_Load()

    g_hMouseHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseProc, App.hInstance, App.ThreadID)
    g_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)

    Sleep 5000

    If g_hMouseHook Then
    Call UnhookWindowsHookEx(g_hMouseHook)
    g_hMouseHook = 0
    End If
    If g_hKeyboardHook Then
    Call UnhookWindowsHookEx(g_hKeyboardHook)
    g_hKeyboardHook = 0
    End If

    End Sub

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''
    'API Constants
    Public Const WH_KEYBOARD As Long = 2
    Public Const WH_MOUSE As Long = 7

    'API Declarations
    Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    'Global mouse/keyboard function callback handles
    Public g_hMouseHook As Long
    Public g_hKeyboardHook As Long

    Public Function MouseProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    'Mouse Function Hook...
    If idHook < 0 Then
    MouseProc = CallNextHookEx(g_hMouseHook, idHook, wParam, ByVal lParam)
    Else
    'Setting the return value to -1 cancels Mouse input...
    MouseProc = -1
    End If
    End Function

    Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    'Keyboard Function Hook...
    If idHook < 0 Then
    KeyboardProc = CallNextHookEx(g_hKeyboardHook, idHook, wParam, ByVal lParam)
    Else
    'Setting the return value to -1 cancels Keyboard input...
    KeyboardProc = -1
    End If
    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''

    Or is there any better suggestion...?
    By the way, I am using VB5

    Thank you.

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Use BlockInput api call.
    Code:
    Private Declare Function BlockInput Lib "user32" (ByVal fBlock As Long) As Long
    
    'to disable input
    BlockInput True
    
    'to enable input
    BlockInput False
    Pass TRUE as the parameter for blocking Mouse and keyboard input, and false for enabling input

  3. #3
    Con
    Guest

    Thank you

    Thank you. But I had tried that too. It only works in win98, but not in win95.
    Any suggestion.....?

  4. #4
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    United States
    Posts
    420

    does it work with nt

    does it work with nt

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    It will work only under Win 98/ME and Windows 2000

  6. #6
    j2k
    Guest

    Re: Thank you

    Originally posted by Con
    Thank you. But I had tried that too. It only works in win98, but not in win95.
    Any suggestion.....?
    Maybe it's time for you to upgrade? I'm sure there are quite a lot of programming benefits from a new OS such as Win98, ME or even 2000 (Built on NT technology).

  7. #7
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    To block Mouse and Keyboard system wide you will need C++ to create a standard dll.

    Also try this:
    Shell "rundll32 mouse,disable"
    Shell "rundll32 keyboard,disable"

    Shell "rundll32 mouse,enable"
    Shell "rundll32 keyboard,enable"

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