Sign in to follow this  
Followers 0
JohnOne

MouseMove with keyboard

5 posts in this topic

#1 ·  Posted (edited)

I remember when I first came to AutoIt and looking at them youtube video tutorials by someone or other and was moving the mouse with keyboard.

Anyway, I don't know what possessed me but I decided to write one myself, I doubt it's the same as what I seen a few years ago.

It's quite basic, and for beginners to look at and figure out what's going on beyond my meagre comments.

It simply moves the mouse with arrow keys, and speeds the move up after a delay.

Esc to Exit.

#include <misc.au3>

; Set hotkeys to same func
HotKeySet("{LEFT}", "_Move")
HotKeySet("{RIGHT}", "_Move")
HotKeySet("{UP}", "_Move")
HotKeySet("{DOWN}", "_Move")
HotKeySet("{Esc}", "_Move")

; Fine Tuning
Global Const $MASTER_SPEED = 5
Global Const $DELAY = 300
Global Const $STEP = 5
Global Const $BEHAVIOUR = 0 ; 0 or 1

While 3
    Sleep(333)
WEnd

Func _Move()

    ; Store hotkey
    Local $HKP = @HotKeyPressed

    ; For _IsPressed
    Local $KeyCode

    ; Unset hotkey
    Switch $HKP
        Case "{DOWN}"
            HotKeySet("{DOWN}")
            $KeyCode = "28"
        Case "{UP}"
            HotKeySet("{UP}")
            $KeyCode = "26"
        Case "{LEFT}"
            HotKeySet("{LEFT}")
            $KeyCode = "25"
        Case "{RIGHT}"
            HotKeySet("{RIGHT}")
            $KeyCode = "27"
        Case "{Esc}"
            Exit
    EndSwitch

    ; Sort some variables
    Local $SPEED = $MASTER_SPEED
    Local $hDll = DllOpen('user32.dll')
    Local $aPOS = MouseGetPos()
    Local $Timer = TimerInit()

    ; While the passed hotkey is down
    While _IsPressed($KeyCode, $hDll)
        If TimerDiff($Timer) > $DELAY And $SPEED > 1 Then
            $Timer = TimerInit()
            If $SPEED > 1 Then
                $SPEED -= 1
            EndIf
        EndIf

        ; Test keycode
        Switch $KeyCode
            Case "28"
                $aPOS[1] += $STEP
                If $aPOS[1] >= @DesktopHeight Then
                    If Not $BEHAVIOUR Then
                        ExitLoop
                    EndIf
                    MouseMove($aPOS[0], 0, 0)
                    $aPOS = MouseGetPos()
                EndIf
            Case "26"
                $aPOS[1] -= $STEP
                If $aPOS[1] <= 0 Then
                    If Not $BEHAVIOUR Then
                        ExitLoop
                    EndIf
                    MouseMove($aPOS[0], @DesktopHeight, 0)
                    $aPOS = MouseGetPos()
                EndIf
            Case "25"
                $aPOS[0] -= $STEP
                If $aPOS[0] <= 0 Then
                    If Not $BEHAVIOUR Then
                        ExitLoop
                    EndIf
                    MouseMove(@DesktopWidth, $aPOS[1], 0)
                    $aPOS = MouseGetPos()
                EndIf
            Case "27"
                $aPOS[0] += $STEP
                If $aPOS[0] >= @DesktopWidth Then
                    If Not $BEHAVIOUR Then
                        ExitLoop
                    EndIf
                    MouseMove(0, $aPOS[1], 0)
                    $aPOS = MouseGetPos()
                EndIf
        EndSwitch

        MouseMove($aPOS[0], $aPOS[1], $SPEED)
    WEnd

    ;Clean up
    DllClose($hDll)

    ; Reset hotkey
    Switch $HKP
        Case "{DOWN}"
            HotKeySet("{DOWN}", "_Move")
        Case "{UP}"
            HotKeySet("{UP}", "_Move")
        Case "{LEFT}"
            HotKeySet("{LEFT}", "_Move")
        Case "{RIGHT}"
            HotKeySet("{RIGHT}", "_Move")
    EndSwitch

EndFunc   ;==>_Move 

EDIT:

Forgot to mention, I left a nice little bug in the code, which can occur when $BEHAVIOUR Const is set to 1.

Edited by JohnOne
1 person likes this

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



#4 ·  Posted (edited)

funny

p.s.

I had to change this code

; Reset hotkey
    Switch $HKP
        Case "{DOWN}"
            HotKeySet("{DOWN}", "DOWN")
        Case "{UP}"
            HotKeySet("{UP}", "UP")
        Case "{LEFT}"
            HotKeySet("{LEFT}", "LEFT")
        Case "{RIGHT}"
            HotKeySet("{RIGHT}", "RIGHT")
    EndSwitch

with this

; Reset hotkey
    Switch $HKP
        Case "{DOWN}"
            HotKeySet("{DOWN}", "_Move")
        Case "{UP}"
            HotKeySet("{UP}", "_Move")
        Case "{LEFT}"
            HotKeySet("{LEFT}", "_Move")
        Case "{RIGHT}"
            HotKeySet("{RIGHT}", "_Move")
    EndSwitch

p.p.s.

maybe disabling the movement of the "text" cursor while moving the mouse cursor could be an improvement

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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