Sign in to follow this  
Followers 0
enneract

Disabling mouse, using scroll lock

7 posts in this topic

I'm working on porting a small script written for AHK, designed to disable mouse input for a short time after any key is pressed. (I brush my trackpad on my laptop quite frequently while typing, very annoying).

I'm using the example from the documentation _WinAPI_SetWindowsHookEX() as a skeleton, and I have it mostly working... except... 1) the only way of disabling the mouse I've found is _MouseTrap(), but I can't figure out how to use this to trap the mouse in a 1 pixel region where it is. 2) I haven't figured out how to disable mouseclicks, 3) my code for using scroll lock as a toggle doesn't seem to work, and I'm lost as to why, 4) I just thought of this, so I haven't actually looked for one yet, but is there a way to detect scroll lock toggle state?

Below is my code (well, only some mine!). If anyone has input as to how to solve the problems listed and are bored\unoccupied enough to share it, I would be appreciative!

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <misc.au3>

Opt('MustDeclareVars', 1)

Global $hHook, $hStub_KeyProc, $buffer = ""
Global $latest = 0, $on = 0

_Main()

Func _Main()
    OnAutoItExitRegister("Cleanup")

    Local $hmod

    $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    $hmod = _WinAPI_GetModuleHandle(0)
    $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)

    Run("Notepad")
    WinWait("Untitled -")
    WinActivate("Untitled -")

    While 1
        If TimerDiff($latest) > 500 Then
            _MouseTrap ()
        EndIf
    WEnd
EndFunc   ;==>_Main

Func EvaluateKey($keycode)
    If ($keycode = 145) Then ; Scroll Lock
        ConsoleWrite("Scroll LOCK! -" & $keycode & "-" & $on)
        If $on = 0 Then $on = 1
        If $on = 1 Then $on = 0
    ElseIf $on = 1 Then
        _MouseTrap(0, 0)
        $latest = TimerInit()
    EndIf
EndFunc   ;==>EvaluateKey

;===========================================================
; callback function
;===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    Else
        EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode"))
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func Cleanup()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>Cleanup

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This works for me, it will probably prove to be useful as well.. (I have a laptop w/ a trackpad also)

#Include <Misc.au3>

Local $MouseTrapped

HotKeySet("{SCROLLLOCK}", "TogMouse")
HotKeySet("!{ESC}", "Quit")

While 1
 Sleep(10)
WEnd

Func TogMouse()
 $MouseTrapped = Not $MouseTrapped
 While $MouseTrapped
  ToolTip('MouseTrapped', 1, 1)
  _MouseTrap(0, 0, 0, 0)
  Sleep(10)
 WEnd
 ToolTip('')
 _MouseTrap()
EndFunc

Func Quit()
 ToolTip('')
 _MouseTrap()
 Exit
EndFunc

I know that _MouseTrap allows clicks thru, but the mouse position is locked at 0,0 so it doesn't really matter, to me anyway..

Edited by snowmaker

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Look at this for disabling mouse clicks: http://www.autoitscript.com/forum/index.php?showtopic=64738

and as for using scroll lock as a toggle... that'd be difficult I think, to detect the state. But something you can do is just make a toggle in AutoIt yourself. Have the hotkey go to a function that sets a variable += 1, and if it = 2 then it's off, and set to 0. If = 1 then it's enabled. And to know the current state of your toggle, just make a Tooltip() in the corner of the screen with like "Mouse off" or something.

Edit: just thinking... with the MouseSetOnEvent UDF above, you wouldn't even need the mouse trap... since I'm assuming your whole issue is that it clicks outside of where you're typing?

Edited by mistersquirrle

RAWR! I'm hungry :( Feed the panda squirrle

Share this post


Link to post
Share on other sites

Look at this for disabling mouse clicks: http://www.autoitscript.com/forum/index.php?showtopic=64738

and as for using scroll lock as a toggle... that'd be difficult I think, to detect the state. But something you can do is just make a toggle in AutoIt yourself. Have the hotkey go to a function that sets a variable += 1, and if it = 2 then it's off, and set to 0. If = 1 then it's enabled. And to know the current state of your toggle, just make a Tooltip() in the corner of the screen with like "Mouse off" or something.

Edit: just thinking... with the MouseSetOnEvent UDF above, you wouldn't even need the mouse trap... since I'm assuming your whole issue is that it clicks outside of where you're typing?

Ayup. Awesome link, thanks, solved my issues.

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <misc.au3>
#include <MouseSetOnEvent_UDF.au3>

Global $hHook, $hStub_KeyProc
Global $latest = 0
Global Const $VK_SCROLL = 0x91, $timeout = 300

_Main()

Func _Main()
    OnAutoItExitRegister("Cleanup")

    $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    $hmod = _WinAPI_GetModuleHandle(0)
    $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)

    While 1
        If TimerDiff($latest) > $timeout OR _GetScrollLock() = 0 Then
            _BlockMouseInput(1)
        EndIf
    WEnd
EndFunc   ;==>_Main

Func _BlockMouseInput($iOpt=0)
    If $iOpt = 0 Then
        _MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_MOVE_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_SECONDARYDBLCLK_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_WHEELDOWN_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_WHEELUP_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_WHEELDBLCLK_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_WHEELSCROLL_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_XBUTTONDOWN_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_XBUTTONUP_EVENT, "__Dummy")
        _MouseSetOnEvent($MOUSE_XBUTTONDBLCLK_EVENT, "__Dummy")
    Else
        _MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT)
        _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT)
        _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT)
        _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT)
        _MouseSetOnEvent($MOUSE_MOVE_EVENT)
        _MouseSetOnEvent($MOUSE_SECONDARYDBLCLK_EVENT)
        _MouseSetOnEvent($MOUSE_WHEELDOWN_EVENT)
        _MouseSetOnEvent($MOUSE_WHEELUP_EVENT)
        _MouseSetOnEvent($MOUSE_WHEELDBLCLK_EVENT)
        _MouseSetOnEvent($MOUSE_WHEELSCROLL_EVENT)
        _MouseSetOnEvent($MOUSE_XBUTTONDOWN_EVENT)
        _MouseSetOnEvent($MOUSE_XBUTTONUP_EVENT)
        _MouseSetOnEvent($MOUSE_XBUTTONDBLCLK_EVENT)
    EndIf
EndFunc   ;==>_BlockMouseClicksInput

Func _GetScrollLock()
    Local $ret
    $ret = DllCall("user32.dll","long","GetKeyState","long",$VK_SCROLL)
    Return $ret[0]
EndFunc

Func EvaluateKey()
    If _GetScrollLock() = 1 Then
        _BlockMouseInput(0)
        $latest = TimerInit()
    EndIf
EndFunc   ;==>EvaluateKey

Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    Else
        EvaluateKey()
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func Cleanup()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>Cleanup

It is ugly but it works. Scroll lock toggles it on and off. You need the UDF include linked in the quoted post.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

lmao, you're right... ugly :mellow: (just a little though) but hey, I've had uglier, and if it work, it works (like my friend said when I clean up 60-some lines of code to 4 for him). Glad you got it working, I'd never use your script though :( I don't hit my track pad nearly enough, and I play games so often it would be a hassle

Edited by mistersquirrle

RAWR! I'm hungry :( Feed the panda squirrle

Share this post


Link to post
Share on other sites

lmao, you're right... ugly :mellow: (just a little though) but hey, I've had uglier, and if it work, it works (like my friend said when I clean up 60-some lines of code to 4 for him). Glad you got it working, I'd never use your script though :( I don't hit my track pad nearly enough, and I play games so often it would be a hassle

Yea... the gaming thing is why I added the toggle. I don't game very often on my tablet, though. That being said, my first version had no toggle, which made me consider that this might be an interesting solution to make gaming on school\company\similar machines impossibly annoying.

Share this post


Link to post
Share on other sites

I changed the code in my earlier post. It toggles now.


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0