Click to See Complete Forum and Search --> : Disabling Mouse and Keyboard
Sha
Nov 8th, 2000, 08:51 AM
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
Shell "rundll32 mouse,disable"
Shell "rundll32 keyboard,disable"
Infojens
Nov 15th, 2000, 07:35 AM
How can i able them again ???
amdimauro
Nov 15th, 2000, 08:32 AM
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...
'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)...
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)...
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
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
To enable them.
Shell "rundll32 mouse,enable"
Shell "rundll32 keyboard,enable"
Infojens
Nov 16th, 2000, 05:46 AM
This codes is not working on my computer:
Shell "rundll32 mouse,enable"
Shell "rundll32 keyboard,enable"
am i missing something ????
amdimauro
Nov 16th, 2000, 08:14 AM
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.
simoyd
May 24th, 2002, 07:14 PM
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)
rocky76259
Dec 6th, 2002, 05:33 PM
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
scr0p
Dec 6th, 2002, 09:30 PM
Dont you need to restart to enable input you blocked with
Shell "rundll32 mouse,disable"
Shell "rundll32 keyboard,disable"
amitabh
Dec 7th, 2002, 07:27 AM
Private Declare Function BlockInput Lib "user32" (ByVal fBlock As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Activate()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
DoEvents
'block the mouse and keyboard input
BlockInput True
'wait 10 seconds before unblocking it
Sleep 10000
'unblock the mouse and keyboard input
BlockInput False
End Sub
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.