Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code to disable both keyboard and mouse temporarily
#1
I need to be able to remotely disable both keyboard and mouse on all of the student computers for a few minutes to carry out certain tasks.

I am already using both Ultra VNC and Specrem to remotely control their machines from my own office. Both programs have the function to disable both keyboard and mouse, however VNC cannot be used to enable and disable all computers at the same time, making it unusable. Specrem can disable all computers at the same time, but the keyboard disable function does not work. I have written to the author but got no response.

So..... Do you have a function or macro to disable both keyboard and mouse and a second macro to enable them again. (or at least keyboard) These macros would be on the student computers and I would start them all remotely through Specrem from my computer.

Thanks in advance

Al
#2
From MSDN:

Quote:BlockInput
The BlockInput function blocks keyboard and mouse input events from reaching applications.

BOOL BlockInput(
BOOL fBlockIt // block option
);
Parmeters
fBlockIt
[in] Specifies the function's purpose. If this parameter is TRUE, keyboard and mouse input events are blocked. If this parameter is FALSE, keyboard and mouse events are unblocked. Note that only the thread that blocked input can successfully unblock input.
Return Values
If the function succeeds, the return value is nonzero.

If input is already blocked, the return value is zero. To get extended error information, call GetLastError.

Remarks
When input is blocked, real physical input from the mouse or keyboard will not affect the input queue's synchronous key state (reported by GetKeyState and GetKeyboardState), nor will it affect the asynchronous key state (reported by GetAsyncKeyState). However, the thread that is blocking input can affect both of these key states by calling SendInput. No other thread can do this.

The system will unblock input in the following cases:

The thread that blocked input unexpectedly exits without calling BlockInput with fBlock set to FALSE. In this case, the system cleans up properly and re-enables input.
Windows 95/98/Me: The system displays the Close Program/Fault dialog box. This can occur if the thread faults or if the user presses CTRL+ALT+DEL.
Windows 2000/XP: The user presses CTRL+ALT+DEL or the system invokes the Hard System Error modal message box (for example, when a program faults or a device fails).
Requirements
Windows NT/2000/XP: Included in Windows 2000 and later.
Windows 95/98/Me: Included in Windows 98 and later.
Header: Declared in Winable.h.
Library: Use User32.lib.
Example:

Code:
Copy      Help
dll# user32 BlockInput fBlockIt
BlockInput 1
60
BlockInput 0
#3
Hi Gintaras

Fantastic. That seems to work. And it's so simple.

Thanks a million.

I'll get back to you.

Al
#4
Unfortunately the students at school have discovered that Cntrl Alt Delete unblocks the keyboard and mouse. Is it possible to temporarily disable Ctrl Alt Delete while the keyboards and mice are locked?
#5
On Windows 98/Me, we can easily disable Ctrl+Alt+Delete. On other OS, we cannot disable it, but can install low level hooks to filter keyboard and mouse input.

BlockInput2 - disable keyboard and/or mouse
#6
Hi Gintaras

Thanks again. The BlockInput2 code works well except for when I try to use:

key Wr

Is there any way of getting that to work.

The other windows keyboard shortcuts seem to work ok. It is only "W" which does not respond.

I am still using win98 on the school computers but will be going over to win2000 pro in 6 months.

Grateful for any hints you might be able to give before Saturday if you have the time. I need to get the computers back on line then.

thanks

Alistair
#7
I just thought of a possible reason.

On the Windows START button the Run button is called Run in English but Kjør in Norwegian. So in Norwegian we would type the letter "k" instead of "r".

Could this help to explain why "key Wr" does not work when BlockInput2 is running?

Al
#8
It is problematic on Windows 98. Maybe try to temporarily unblock input, or only Windows hotkeys, as in example.

Code:
Copy      Help
BlockInput2 1
1
SystemParametersInfo(0x0061 0 &_i 0)
key Wr
SystemParametersInfo(0x0061 1 &_i 0)
1
BlockInput2 0
#9
Hi Gintaras

You sent me the following code 2 years ago which worked well then. But now I am trying to run it under Win2000 on QM 2.16 and it doesn't work. The mouse becomes very slow and jumpy but not frozen. I have not tried the keyboard.

You may recall that I do not want the students to be able to use Ctrl-Alt-Del to defeat the code.

Grateful for any help you can give.

Alistair


Gintaras Wrote:On Windows 98/Me, we can easily disable Ctrl+Alt+Delete. On other OS, we cannot disable it, but can install low level hooks to filter keyboard and mouse input.

Note: the attached file contains newer versions of these functions. Just import and use.

Function BlockInput2:
Code:
Copy      Help
function block

;Blocks keyboard and mouse input so that Ctrl+Alt+Delete cannot unblock.

;EXAMPLE
;opt waitmsg 1
;BlockInput2 1
;60
;BlockInput2 0


dll user32 #SetWindowsHookEx idHook lpfn hmod dwThreadId
dll user32 #UnhookWindowsHookEx hHook
dll user32 #CallNextHookEx hHook ncode wParam !*lParam
type KBDLLHOOKSTRUCT vkCode scanCode flags time dwExtraInfo
type MSLLHOOKSTRUCT POINT'pt mouseData flags time dwExtraInfo
def WH_KEYBOARD_LL 13
def WH_MOUSE_LL 14
def SPI_SETSCREENSAVERRUNNING 0x0061

if(_winnt)
,int+ bikhook bimhook
,if(block)
,,if(bikhook) UnhookWindowsHookEx(bikhook)
,,if(bimhook) UnhookWindowsHookEx(bimhook)
,,bikhook=SetWindowsHookEx(WH_KEYBOARD_LL &BIKeyboardProc _hinst 0)
,,bimhook=SetWindowsHookEx(WH_MOUSE_LL &BIMouseProc _hinst 0)
,else
,,UnhookWindowsHookEx(bikhook); bikhook=0
,,UnhookWindowsHookEx(bimhook); bimhook=0
else
,SystemParametersInfo(SPI_SETSCREENSAVERRUNNING block &_i 0)
,BlockInput block

It also requires two other functions that filter input events on Windows 2000/XP. System calls them on keyboard and mouse input events.

BIKeyboardProc:
Code:
Copy      Help
;/
function nCode wParam KBDLLHOOKSTRUCT*lParam

if(nCode>=0)
,if(lParam.flags&16=0) ret 1

ret CallNextHookEx(bikhook nCode wParam lParam)

BIMouseProc:
Code:
Copy      Help
;/
function nCode wParam MSLLHOOKSTRUCT*lParam

if(nCode>=0)
,if(lParam.flags&1=0) ret 1

ret CallNextHookEx(bimhook nCode wParam lParam)
#10
Before BlockInput2, insert

opt waitmsg 1

You can download the attachment (above). It is the same, with little enhancements, and does not require opt waitmsg 1.

If it does not work properly even with opt waitmsg 1, it means that thread that calls BlockInput2 for some reason cannot process messages. Then you have to call BlockInput2 from another thread.

This macro blocks input, launches the real macro "Function14" (it must be function), and waits until the real macro exits:

Code:
Copy      Help
opt waitmsg 1
BlockInput2 3
wait 0 H mac("Function14")
BlockInput2 0

BlockInput2 in other thread cannot be used on Win 98/me.
#11
I downloaded the code file called Block. As you said it worked fine without "opt waitmsg 1".

I also used the "function14" code. It also worked.

However, both of the above did not block Ctrl-Alt-Del.

Remember I use win2000.

Have I misunderstood something here.

-------------------------------

I have used both of the following with and without "opt waitmsg 1"
:

BlockInput2 3
5
BlockInput2 0

and

BlockInput2 3
wait 0 H mac("Function14")
BlockInput2 0

(with a 5 second wait in "Function14")
#12
It cannot disable Ctrl+Alt+Delete, but Ctrl+Alt+Delete does not unblock keyboard and mouse.

You can try to edit the keyboard hook function so that it would detect Ctrl+Alt+Delete, then wait for the Windows Task Manager window, and close it.
#13
Thanks for the lightning fast response again. Much appreciated.

Interesting suggestion.

Al
#14
I wrote a function to automatically close the Task Manager Window which worked. However any macros running in the background did not run reliably after Ctrl Alt Del were pressed.

Would the following be a better option??

Could I temporarily reassign the Del key as another key? "§" for example. (only for the period the macro is running. I would reassign it at the end)
So when a student pressed Ctrl Alt Del they would in reality press Ctrl Alt §, which hopefully would trigger nothing.

Would this change affect the command "key X" in the running macro?
#15
I don't know how to reassign the Delete key so that Ctrl+Alt+Del would not work.
#16
Well that's the first time you have not been able to answer a question.

Thanks for all your effort.


Forum Jump:


Users browsing this thread: 1 Guest(s)