Results 1 to 11 of 11

Thread: Disabling Mouse and Keyboard

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Location
    NC
    Posts
    2

    Question

    I would like to know if there is a way to disable the mouse and keyboard from within VB while a certain process runs, and then enable them again once complete. This must be system wide so the mouse and keyboard cannot bring focus to another application or anything.

    Any help with this would be greatly appreciated since I have no idea of how to do this (or if it is possible).

    Thanks in Advance

  2. #2
    Guest
    Code:
    Shell "rundll32 mouse,disable"
    Shell "rundll32 keyboard,disable"

  3. #3
    New Member
    Join Date
    Nov 2000
    Posts
    3

    Disabling Mouse and Keyboard

    How can i able them again ???
    Infojens

  4. #4
    New Member
    Join Date
    Nov 2000
    Posts
    7
    What you actually should use is the SetWindowsHookEx API call.
    You use this API call to replace the VB Mouse and Keyboard processing routines with your own,
    then you can do what you want with the events...and you can easily restore the normal Keyboard and Mouse Event handlers
    using the UnhookWindowsHookEx API call. Remember to restore the Mouse and Keyboard handlers or you app will crash.

    Here's the code

    The definitions...
    Code:
    '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
    
    'Global mouse/keyboard function callback handles
    Public g_hMouseHook As Long
    Public g_hKeyboardHook As Long
    The calls to set the Mouse and Keyboard Callback routines (can be done anywhere, Form, Class, Module)...
    Code:
    g_hMouseHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseProc, App.hInstance, App.ThreadID)
    g_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
    The calls to reset the VB Mouse and Keyboard Callback routines (can be done anywhere, Form, Class, Module)...
    Code:
    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

    NOW...your Mouse and Keyboard callback routines...these can ONLY be in a .BAS Code Module
    Code:
    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

  5. #5
    Guest
    To enable them.
    Code:
    Shell "rundll32 mouse,enable"
    Shell "rundll32 keyboard,enable"

  6. #6
    New Member
    Join Date
    Nov 2000
    Posts
    3

    This is not working on my computer

    This codes is not working on my computer:

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

    am i missing something ????
    Infojens

  7. #7
    New Member
    Join Date
    Nov 2000
    Posts
    7
    My suggestion is to use the code that I provided.
    It gives you much more control over the Mouse and Keyboard input...and it works.

  8. #8
    Member
    Join Date
    Jan 2002
    Posts
    45
    this doesn't seem to work system wide... only on the vb app... is there someway to make it system wide (the vb code not the run blah blah, enable crap)

  9. #9
    Lively Member
    Join Date
    Jun 2002
    Posts
    110
    instead of app.threadid in the two api calls, type 0 make sur eu leave the comma and parenthesis there... just take out app.threadid and type 0

  10. #10
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    Dont you need to restart to enable input you blocked with

    Shell "rundll32 mouse,disable"
    Shell "rundll32 keyboard,disable"
    asdf

  11. #11
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    VB Code:
    1. Private Declare Function BlockInput Lib "user32" (ByVal fBlock As Long) As Long
    2. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    3. Private Sub Form_Activate()
    4.     'KPD-Team 2000
    5.     'URL: [url]http://www.allapi.net/[/url]
    6.     'E-Mail: [email]KPDTeam@Allapi.net[/email]
    7.     DoEvents
    8.     'block the mouse and keyboard input
    9.     BlockInput True
    10.     'wait 10 seconds before unblocking it
    11.     Sleep 10000
    12.     'unblock the mouse and keyboard input
    13.     BlockInput False
    14. End Sub

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