Snippets ( AutoIt Mouse & Keyboard ): Difference between revisions
Jump to navigation
Jump to search
m (+Snippets Category (+ normalize top)) |
mNo edit summary |
||
Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
[[category:Snippets]] | [[category:Snippets]] | ||
{{Snippet Credit Header}} | {{Snippet Credit Header}} | ||
= | {{Snippet Header | ||
|UDFName=Control Button by HotKey | |||
|AuthorURL=4813-smoke-n | |||
|AuthorName=SmOke_N | |||
|ReturnToTop=1 | |||
|Desc=http://www.autoitscript.com/forum/index.php?s=&showtopic=36564&view=findpost&p=270412 | |||
|AutoItCode= | |||
; Control Button by HotKey | ; Control Button by HotKey | ||
; http://www.autoitscript.com/forum/index.php?s=&showtopic=36564&view=findpost&p=270412 | ; http://www.autoitscript.com/forum/index.php?s=&showtopic=36564&view=findpost&p=270412 | ||
HotKeySet("+4", "_ClickButton") | |||
Global Const $Main = GUICreate("Some GUI", 200, 100) | |||
Global Const $Button = GUICtrlCreateButton("Some Button To Click", 10, 35, 180, 30) | |||
Global $fClickit | |||
GUISetState(@SW_SHOWNORMAL) | |||
While 1 | While 1 | ||
Switch GUIGetMsg() | Switch GUIGetMsg() | ||
Line 21: | Line 30: | ||
Case $Button | Case $Button | ||
If Not $fClickit Then | If Not $fClickit Then | ||
MsgBox(64, | MsgBox(64, "Clicked", "You clicked the button") | ||
Else | Else | ||
$fClickit = Not $fClickit | $fClickit = Not $fClickit | ||
MsgBox(64, | MsgBox(64, "Clicked", "You used a hotkey to click the button") | ||
EndIf | EndIf | ||
EndSwitch | EndSwitch | ||
WEnd | WEnd | ||
Func _ClickButton() | Func _ClickButton() | ||
$fClickit = Not $fClickit | $fClickit = Not $fClickit | ||
ControlClick(HWnd($Main), '', $Button) | ControlClick(HWnd($Main), '', $Button) | ||
EndFunc | EndFunc | ||
}} | |||
== | {{Snippet Header | ||
|UDFName=_ControlMouseClick | |||
|AuthorURL=58637-fett8802 | |||
|AuthorName=fett8802 | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
; #FUNCTION# ==================================================================================================================== | ; #FUNCTION# ==================================================================================================================== | ||
; Name...........: _ControlMouseClick | ; Name...........: _ControlMouseClick | ||
; Description ...: Use the mouse to move to a control and click it | ; Description ...: Use the mouse to move to a control and click it | ||
; Syntax.........: _ControlMouseClick($iTitle, $iText, $iControl [, $iButton = "left" [, $iClicks = "1" [, $iSpeed = "10" [, $iXpos = | ; Syntax.........: _ControlMouseClick($iTitle, $iText, $iControl [, $iButton = "left" [, $iClicks = "1" [, $iSpeed = "10" [, $iXpos = '' [, $iYpos = '' ]]]]] ) | ||
; Parameters ....: $iTitle - The title of the window containing the control | ; Parameters ....: $iTitle - The title of the window containing the control | ||
; $iText - Some text from the window containing the control. Can enter no text be using | ; $iText - Some text from the window containing the control. Can enter no text be using '' | ||
; $iControl - The Control ID of the control to click | ; $iControl - The Control ID of the control to click | ||
; $iButton - [optional] The button to click: "left", "right", "middle", "main", "menu", "primary", "secondary". Default is "left" | ; $iButton - [optional] The button to click: "left", "right", "middle", "main", "menu", "primary", "secondary". Default is "left" | ||
Line 52: | Line 66: | ||
; UserCallTip....: _ControlMouseClick ( "title", "text", controlID [, button [, clicks [, speed [, x [, y ]]]]] ) Use the mouse to move to a control and click it.(required: #include <KrisUDF.au3>) | ; UserCallTip....: _ControlMouseClick ( "title", "text", controlID [, button [, clicks [, speed [, x [, y ]]]]] ) Use the mouse to move to a control and click it.(required: #include <KrisUDF.au3>) | ||
; =============================================================================================================================== | ; =============================================================================================================================== | ||
Func _ControlMouseClick($iTitle, $iText, $iControl, $iButton = "left", $iClicks = "1", $iSpeed = "10", $iXpos = '', $iYpos = '') | |||
Func _ControlMouseClick($iTitle, $iText, $iControl, $iButton = "left", $iClicks = "1", $iSpeed = "10", $iXpos = | |||
$iOriginal = Opt("MouseCoordMode", 2) ;Change the MouseCoordMode to relative coords and get the previous setting | $iOriginal = Opt("MouseCoordMode", 2) ;Change the MouseCoordMode to relative coords and get the previous setting | ||
$aPos = ControlGetPos($iTitle, $iText, $iControl) ;Get the position of the given control | $aPos = ControlGetPos($iTitle, $iText, $iControl) ;Get the position of the given control | ||
Line 59: | Line 72: | ||
Opt("MouseCoordMode", $iOriginal) ;Change the MouseCoordMode back to the original | Opt("MouseCoordMode", $iOriginal) ;Change the MouseCoordMode back to the original | ||
EndFunc ;==>_ControlMouseClick | EndFunc ;==>_ControlMouseClick | ||
}} | |||
= | {{Snippet Header | ||
|UDFName=Get Mouse Position | |||
|AuthorURL=33528-shmuelw1 | |||
|AuthorName=shmuelw1 | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
; This script returns the current mouse position | ; This script returns the current mouse position | ||
; Edit the value of $mode as required | ; Edit the value of $mode as required | ||
#include <Misc.au3> ; required for _IsPressed | |||
Global $mode = 0 ; this sets the MouseCoordMode - see Opt("MouseCoordMode" later in the script | |||
Global $modeText | |||
Global $pos | |||
Opt("MouseCoordMode", $mode) ; 1 = absolute screen position, 0 = relative to active windows, 2 = relative to client area | |||
Opt("TrayIconDebug", 1) | Opt("TrayIconDebug", 1) | ||
Line 83: | Line 102: | ||
ToolTip("Move the pointer to the desired location. Press Enter to continue.", @DesktopWidth/2-250, @DesktopHeight/2-10) | ToolTip("Move the pointer to the desired location. Press Enter to continue.", @DesktopWidth/2-250, @DesktopHeight/2-10) | ||
While Not _IsPressed("0D") ; wait for Enter key | While Not _IsPressed("0D") ; wait for Enter key | ||
Sleep(100) | Sleep(100) | ||
WEnd | WEnd | ||
ToolTip( | |||
ToolTip('') ; close ToolTip | |||
$pos = MouseGetPos() | $pos = MouseGetPos() | ||
MsgBox(0, "Mouse Position", "The mouse position " & $modeText & $pos[0] & "," & $pos[1] & " (x,y)") | MsgBox(0, "Mouse Position", "The mouse position " & $modeText & $pos[0] & "," & $pos[1] & " (x,y)") | ||
}} | |||
= | {{Snippet Header | ||
|UDFName=Get Mouse Position With Hotkey | |||
|AuthorURL=33528-shmuelw1 | |||
|AuthorName=shmuelw1 | |||
|ReturnToTop=1 | |||
|Desc=This script returns the current mouse position | |||
|AutoItCode= | |||
; This script returns the current mouse position | ; This script returns the current mouse position | ||
; Edit the value of $mode as required | ; Edit the value of $mode as required | ||
#include < | #include <Misc.au3> ; required for _IsPressed | ||
Global $mode = 0 ; this sets the MouseCoordMode - see Opt("MouseCoordMode" later in the script | |||
Global $modeText | |||
Global $pos | |||
Opt("MouseCoordMode", $mode) ;1=absolute screen position, 0=relative to active windows, 2= relative to client area | Opt("MouseCoordMode", $mode) ;1=absolute screen position, 0=relative to active windows, 2= relative to client area | ||
Opt("TrayIconDebug", 1) | Opt("TrayIconDebug", 1) | ||
HotKeySet("^#s", "_GetPosition") ; ! = ALT keystroke, + = SHIFT, ^ = CONTROL, # = Windows key | HotKeySet("^#s", "_GetPosition") ; ! = ALT keystroke, + = SHIFT, ^ = CONTROL, # = Windows key | ||
Line 120: | Line 150: | ||
Func _GetPosition() | Func _GetPosition() | ||
ToolTip("Move the pointer to the desired location. Press Enter to continue.", @DesktopWidth/2-250, @DesktopHeight/2-10) | ToolTip("Move the pointer to the desired location. Press Enter to continue.", @DesktopWidth/2-250, @DesktopHeight/2-10) | ||
While Not _IsPressed("0D") ; wait for Enter key | |||
Sleep(100) | Sleep(100) | ||
WEnd | WEnd | ||
ToolTip( | |||
ToolTip('') ; close ToolTip | |||
$pos = MouseGetPos() | $pos = MouseGetPos() | ||
MsgBox(0, "Mouse Position", "The mouse position " & $modeText & $pos[0] & "," & $pos[1] & " (x,y)") | MsgBox(0, "Mouse Position", "The mouse position " & $modeText & $pos[0] & "," & $pos[1] & " (x,y)") | ||
EndFunc | EndFunc | ||
}} | |||
{{Snippet Header | |||
|UDFName=_IsLeftHandedMouse | |||
|AuthorURL=35302-guinness | |||
|AuthorName=guinness | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
#include <WinAPI.au3> | #include <WinAPI.au3> | ||
#include <WindowsConstants.au3> | #include <WindowsConstants.au3> | ||
Line 142: | Line 178: | ||
Return _WinAPI_GetSystemMetrics($SM_SWAPBUTTON) > 0 | Return _WinAPI_GetSystemMetrics($SM_SWAPBUTTON) > 0 | ||
EndFunc ;==>_IsLeftHandedMouse | EndFunc ;==>_IsLeftHandedMouse | ||
}} | |||
{{Snippet Header | |||
|UDFName=_IsRightHandedMouse | |||
|AuthorURL=35302-guinness | |||
|AuthorName=guinness | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
#include <WinAPI.au3> | |||
#include <WindowsConstants.au3> | |||
#include <WinAPI.au3> | #include <WinAPI.au3> | ||
Line 151: | Line 198: | ||
Return _WinAPI_GetSystemMetrics($SM_SWAPBUTTON) = 0 | Return _WinAPI_GetSystemMetrics($SM_SWAPBUTTON) = 0 | ||
EndFunc ;==>_IsRightHandedMouse | EndFunc ;==>_IsRightHandedMouse | ||
}} | |||
{{Snippet Header | |||
|UDFName=Is Mouse Over GUI | |||
|AuthorURL=20477-mrcreator | |||
|AuthorName=MrCreatoR | |||
|ModifierURL=4920-valuater | |||
|ModifierName=Valuater | |||
|ReturnToTop=1 | |||
|Desc=Check if Mouse is over a GUI | |||
|AutoItCode= | |||
; Check if Mouse is over a GUI | ; Check if Mouse is over a GUI | ||
#include <GUIConstants.au3> | #include <GUIConstants.au3> | ||
Global $ParentWin = GUICreate("GetHoveredHwnd") | |||
GUISetState() | |||
GUISetState(@SW_SHOWNORMAL) | |||
While 1 | While 1 | ||
Line 169: | Line 223: | ||
ToolTip("You are over the GUI") | ToolTip("You are over the GUI") | ||
Else | Else | ||
ToolTip( | ToolTip('') | ||
EndIf | EndIf | ||
WEnd | WEnd | ||
Line 178: | Line 232: | ||
Return SetError(1, 0, 0) | Return SetError(1, 0, 0) | ||
EndFunc | EndFunc | ||
}} | |||
{{Snippet Header | |||
|UDFName=_IsMouseInstalled | |||
|AuthorURL=35302-guinness | |||
|AuthorName=guinness | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
#include <WinAPI.au3> | #include <WinAPI.au3> | ||
#include <WindowsConstants.au3> | #include <WindowsConstants.au3> | ||
Line 191: | Line 249: | ||
Return _WinAPI_GetSystemMetrics($SM_CMOUSEBUTTONS) > 0 | Return _WinAPI_GetSystemMetrics($SM_CMOUSEBUTTONS) > 0 | ||
EndFunc ;==>_IsMouseInstalled | EndFunc ;==>_IsMouseInstalled | ||
}} | |||
{{Snippet Header | |||
|UDFName=_IsMouseWheelPresent | |||
|AuthorURL=35302-guinness | |||
|AuthorName=guinness | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
#include <WinAPI.au3> | #include <WinAPI.au3> | ||
#include <WindowsConstants.au3> | #include <WindowsConstants.au3> | ||
Line 204: | Line 266: | ||
Return _WinAPI_GetSystemMetrics($SM_MOUSEWHEELPRESENT) > 0 | Return _WinAPI_GetSystemMetrics($SM_MOUSEWHEELPRESENT) > 0 | ||
EndFunc ;==>_IsMouseWheelPresent | EndFunc ;==>_IsMouseWheelPresent | ||
}} | |||
== | {{Snippet Header | ||
|UDFName=_MouseRepel | |||
|AuthorURL=6946-the-kandie-man | |||
|AuthorName=The Kandie Man | |||
|ReturnToTop=1 | |||
|Desc=Mouse repel - keep mouse away from an area | |||
|AutoItCode= | |||
; GUI snap to corners | |||
; Mouse repel - keep mouse away from an area | |||
; Author - The Kandie Man | |||
Global Const $GUI = GUICreate("Can't Touch This", 200, 100, 200, 200) | |||
GUISetState(@SW_SHOWNORMAL) | |||
AdlibRegister("CallMouseRepel", 10) | AdlibRegister("CallMouseRepel", 10) | ||
Line 223: | Line 290: | ||
Func CallMouseRepel() | Func CallMouseRepel() | ||
Local $coords = WinGetPos($GUI) | Local Const $coords = WinGetPos($GUI) | ||
_MouseRepel($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3]) | _MouseRepel($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3]) | ||
EndFunc ;==>CallMouseRepel | EndFunc ;==>CallMouseRepel | ||
Line 238: | Line 305: | ||
; It is therefore recommended that you call this function from another function that is called by AdlibEnable every 1 to 50ms. | ; It is therefore recommended that you call this function from another function that is called by AdlibEnable every 1 to 50ms. | ||
;=============================================================================== | ;=============================================================================== | ||
Func _MouseRepel($i_left = 0, $i_top = 0, $i_right = 0, $i_bottom = 0) | Func _MouseRepel($i_left = 0, $i_top = 0, $i_right = 0, $i_bottom = 0) | ||
Local $a_MousePos = MouseGetPos() | Local $a_MousePos = MouseGetPos() | ||
Local $i_XCordinate = -1, $i_YCordinate = -1 | Local $i_XCordinate = -1, $i_YCordinate = -1 | ||
If $a_MousePos[0] >= $i_left And $a_MousePos[0] <= $i_right Then | If $a_MousePos[0] >= $i_left And $a_MousePos[0] <= $i_right Then | ||
If ($a_MousePos[0] - $i_left) < ($i_right - $a_MousePos[0]) Then | If ($a_MousePos[0] - $i_left) < ($i_right - $a_MousePos[0]) Then | ||
Line 249: | Line 317: | ||
EndIf | EndIf | ||
EndIf | EndIf | ||
If $a_MousePos[1] >= $i_top And $a_MousePos[1] <= $i_bottom Then | If $a_MousePos[1] >= $i_top And $a_MousePos[1] <= $i_bottom Then | ||
If ($a_MousePos[1] - $i_top) < ($i_bottom - $a_MousePos[1]) Then | If ($a_MousePos[1] - $i_top) < ($i_bottom - $a_MousePos[1]) Then | ||
Line 256: | Line 325: | ||
EndIf | EndIf | ||
EndIf | EndIf | ||
If $i_XCordinate <> -1 And $i_YCordinate <> -1 Then | If $i_XCordinate <> -1 And $i_YCordinate <> -1 Then | ||
If Abs($i_XCordinate - $a_MousePos[0]) > Abs($i_YCordinate - $a_MousePos[1]) Then | If Abs($i_XCordinate - $a_MousePos[0]) > Abs($i_YCordinate - $a_MousePos[1]) Then | ||
Line 266: | Line 336: | ||
EndIf | EndIf | ||
EndFunc ;==>_MouseRepel | EndFunc ;==>_MouseRepel | ||
}} | |||
== | {{Snippet Header | ||
|UDFName=_MyMouseMove | |||
|AuthorURL=3602-martin | |||
|AuthorName=martin | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
; Work with any screen resolution | ; Work with any screen resolution | ||
; This is for the 1440 x 900 screen - change as needed | ; This is for the 1440 x 900 screen - change as needed | ||
Global $iHeight = 900 | Global Const $iHeight = 900 | ||
Global Const $iWidth = 1440 | |||
_MyMouseMove(100, 200) | _MyMouseMove(100, 200) | ||
Line 281: | Line 356: | ||
Return MouseMove(@DesktopWidth * $iX / $iWidth, @DesktopHeight * $iY / $iHeight) | Return MouseMove(@DesktopWidth * $iX / $iWidth, @DesktopHeight * $iY / $iHeight) | ||
EndFunc ;==>_MyMouseMove | EndFunc ;==>_MyMouseMove | ||
}} | |||
{{Snippet Header | |||
|UDFName=Restricted Input Keys - All USB Devices | |||
= | |AuthorURL= | ||
|AuthorName=Multiple Authors | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
#include <GUIConstantsEx.au3> | #include <GUIConstantsEx.au3> | ||
#include <WindowsConstants.au3> | #include <WindowsConstants.au3> | ||
Line 296: | Line 374: | ||
Func Example() | Func Example() | ||
Local $hGUI = GUICreate("Input Filter", 300, 30, -1, -1) | Local Const $hGUI = GUICreate("Input Filter", 300, 30, -1, -1) | ||
$iInput = GUICtrlCreateInput( | |||
$iInput = GUICtrlCreateInput('', 5, 5, 290) | |||
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND") | GUIRegisterMsg($WM_COMMAND, "WM_COMMAND") | ||
GUISetState(@ | |||
GUISetState(@SW_SHOWNORMAL, $hGUI) | |||
While 1 | While 1 | ||
Line 307: | Line 388: | ||
EndSwitch | EndSwitch | ||
WEnd | WEnd | ||
GUIDelete($hGUI) | GUIDelete($hGUI) | ||
EndFunc ;==>Example | EndFunc ;==>Example | ||
Line 313: | Line 395: | ||
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) | Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) | ||
#forceref $hWnd, $iMsg, $wParam, $lParam | #forceref $hWnd, $iMsg, $wParam, $lParam | ||
Return GUICtrlSetData($iInput, StringRegExpReplace(GUICtrlRead($iInput), '[\\/ :* ?"<>\|]', | Return GUICtrlSetData($iInput, StringRegExpReplace(GUICtrlRead($iInput), '[\\/ :* ?"<>\|]', '')) | ||
EndFunc ;==>WM_COMMAND | EndFunc ;==>WM_COMMAND | ||
}} | |||
{{Snippet Header | |||
|UDFName=Two Tray Mouse Menus | |||
|AuthorURL=13029-smashly | |||
|AuthorName=smashly | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
; Two Tray Menus - Right & Left Click On Icon | ; Two Tray Menus - Right & Left Click On Icon | ||
Line 331: | Line 417: | ||
Global $Tray[11], $state = 2 | Global $Tray[11], $state = 2 | ||
TrayCreateItem( | TrayCreateItem('') | ||
TrayCreateItem("Exit") | TrayCreateItem("Exit") | ||
TrayItemSetOnEvent(-1, "TrayEvent") | TrayItemSetOnEvent(-1, "TrayEvent") | ||
Line 340: | Line 426: | ||
While 1 | While 1 | ||
Sleep(100) | Sleep(100) | ||
WEnd | WEnd | ||
Func TrayMenuLeftClick() | Func TrayMenuLeftClick() | ||
Line 347: | Line 433: | ||
For $i = 6 to 10 | For $i = 6 to 10 | ||
TrayItemDelete($Tray[$i]) | TrayItemDelete($Tray[$i]) | ||
Next | Next | ||
For $i = 1 to 5 | For $i = 1 to 5 | ||
$Tray[$i] = TrayCreateItem("1st Menu Item - " & $i, -1, $i -1 ) | $Tray[$i] = TrayCreateItem("1st Menu Item - " & $i, -1, $i -1 ) | ||
Line 366: | Line 452: | ||
Next | Next | ||
EndIf | EndIf | ||
EndFunc | EndFunc | ||
Func TrayEvent() | Func TrayEvent() | ||
MsgBox(0, | MsgBox(0, '', TrayItemGetText(@TRAY_ID)) | ||
If TrayItemGetText(@TRAY_ID) = "Exit" Then Exit | If TrayItemGetText(@TRAY_ID) = "Exit" Then Exit | ||
EndFunc | EndFunc | ||
}} | |||
== | {{Snippet Header | ||
|UDFName=_WinAPI_SwapMouseButton | |||
|AuthorURL=35302-guinness | |||
|AuthorName=guinness | |||
|ReturnToTop=1 | |||
|Desc= | |||
|AutoItCode= | |||
Example() | Example() | ||
Line 398: | Line 489: | ||
Return $aReturn[0] | Return $aReturn[0] | ||
EndFunc ;==>_WinAPI_SwapMouseButton | EndFunc ;==>_WinAPI_SwapMouseButton | ||
}} | |||
Revision as of 17:09, 12 November 2012
Author: SmOke_N
http://www.autoitscript.com/forum/index.php?s=&showtopic=36564&view=findpost&p=270412
Author: fett8802
Author: shmuelw1
Author: shmuelw1
This script returns the current mouse position
Author: guinness
Author: guinness
Author: MrCreatoR
Modified: Valuater
Check if Mouse is over a GUI
Author: guinness
Author: guinness
Author: The Kandie Man
Mouse repel - keep mouse away from an area
Author: martin
Multiple Authors
Author: smashly
Author: guinness