switch off keyboard and mouse input

Chris Davison

MrExcel MVP
Joined
Feb 15, 2002
Messages
1,790
Morning all and Merry Xmas,

some of my stupid clients insist on still trying to type and mouseclick whilst a macro is running that uses SENDKEYS and MOUSECLICK input..... obviously, this messes up the macro and it goes in the wrong direction or crashes

is it possible to disable any manual keyboard input and mouse clicking input at the beginning of the macro and switch them back on at the end of the macro so they can't do this ?

many thanks
Chris
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This API function will hide the mouse Cursor...

Code:
Declare Function ShowCursor& Lib "user32" (ByVal bShow As Long)
 
'Hide mouse cursor
Call ShowCursor&(0)
 
'Show mouse cursor
Call ShowCursor&(1)

Caveat! This hides the cursor from ALL open applications.

What are you using sendkeys for?

SendMessage API may be a better alternative.

Chuck[/url]
 
Upvote 0
Public inputMsg As String
Sub BlockInPut()
'Stop input for mouse only on message!

'Turn off number pads ENTER key.
Application.OnKey "{ENTER}", "myMsg"

'Turn off regular ENTER key.
Application.OnKey "~", "myMsg"

'Turn off all character keys.
Application.Interactive = False

'GoTo myMsg Sub.
myMsg
End Sub

Sub myMsg()
Dim Note
'Display Message box.
inputMsg = "Your Keyboard Input has been Stopped! Please use you Mouse to Respond!"
Note = MsgBox(inputMsg, vbOKCancel + vbDefaultButton2 + vbCritical, "Your Stopped!")
If Note = vbCancel Then
BlockInPut
End If
'GoTo RestoreInPut Sub.
RestoreInPut
End Sub

Sub RestoreInPut()
'Turn on all keys!
Application.Interactive = True
Application.OnKey "{ENTER}"
Application.OnKey "~"
End Sub

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
RestoreInPut
End Sub



Then code like this:


Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
'ThisWorkbook module code!

If Sh.Name = "Sheet2" Then
Cancel = True
End If

End Sub


Will stop the mouse Right-Click on Sheet2!
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top