Temporarily block user keyboard and mouse input

Add-in Express™ Support Service
That's what is more important than anything else

Temporarily block user keyboard and mouse input
How to block user input 
equityfund


Guest


Hi,

I need a way to temporarily disable user keyboard and mouse input.
I've tried the following:

First we define a function:

Friend Declare Function BlockInput Lib "user32" Alias "BlockInput" (ByVal fBlock As Boolean) As Boolean


Then I call this function as (it is wrapped by a Try/Catch/Finally statement):


BlockInput(True) ' Block input
...Do some work...
BlockInput(False) ' Enable input


The BlockInput statements are executes in a background thread.
Here is the link to the MSDN description:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646290(v=vs.85).aspx

I have made successful tests in a simple test form using this code, but for some reason it won't work in my AddIn. In the test case I made it worked both from the main thread and from a separate thread.

Thank you.

Update:
I tested this in my AddIn:

Try
    MsgBox(BlockInput(True))
    Dim lastError As String = Err.LastDllError
    MsgBox(lastError)
    System.Threading.Thread.Sleep(5000)
Catch ex As Exception

Finally
    BlockInput(False)
End Try


The first messagebox gives "False", and the second one gives 5.
I looked it up here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
Apparently 5 means ERROR_ACCESS_DENIED.
Posted 16 Aug, 2014 12:03:25 Top
Andrei Smolin


Add-in Express team


Posts: 18823
Joined: 2006-05-11
Hello,

The last comment at that page as well as ERROR_ACCESS_DENIED suggest that you need to run the host application from the elevated Command Prompt.


Andrei Smolin
Add-in Express Team Leader
Posted 18 Aug, 2014 03:44:15 Top
equityfund


Guest


Hi,

The comment says "on Vista", but I guess it is possible it applies to Win 7/8 as well.
But the Host Application is Excel I assume? It works when I create a simple windows form project (i.e. not an ADX AddIn), and I don't start that program in any special way.

Do you know if there is any other method for blocking user input?

Thank you.
Posted 18 Aug, 2014 12:17:25 Top
Andrei Smolin


Add-in Express team


Posts: 18823
Joined: 2006-05-11
equityfund writes:
The comment says "on Vista", but I guess it is possible it applies to Win 7/8 as well.


Yes.

equityfund writes:
But the Host Application is Excel I assume?


Yes.

equityfund writes:
I don't start that program in any special way.


I assume that UAC is turned on. If so, I suppose that you start the program from Visual Studio running with elevated permissions. If so, try running the .EXE file instead. The main goal is running the progam with non-elevated permissions; I expect that the BlockInput method will fire an exception in this case.

equityfund writes:
Do you know if there is any other method for blocking user input?


Another strategy would be to undo all user actions.


Andrei Smolin
Add-in Express Team Leader
Posted 19 Aug, 2014 04:06:44 Top
equityfund


Guest


Hi Andrei,

You are right, I did execute the program through Visual Studio.
I will try to run the .exe instead and see if I get an exception.
Posted 19 Aug, 2014 09:31:08 Top