AutoHotKey脚本:应用实例

AutoHotKey脚本:应用实例

这些代码来源于AutoHotKey论坛小众软件yonkenHELFEE的博客。这里只有一些内容很少的代码。部分翻译的中文帮助文件:下载

; 此为注释符号,不执行   多行注释 /*          */

修改脚本之后要重新载入脚本,为方便重新载入

代码如下 快捷键为按住左键后 按R           非所有脚本都能加入该语句

 

~LButton & r::

 

reload

return

非安装(绿色)版的AutoHotKey关于.ahk文件的关联

文件夹选项>文件类型>新建 新建ahk>在文件类型中选中ahk>高级>操作 新建

新操作

open   浏览 选择 AutoHotkey.exe   c:/ahk/AutoHotkey.exe "%1"   并设为默认操作

edit    浏览 选择    记事本          C:/WINDOWS/NOTEPAD.EXE "%1"

对于常用编辑器的语法高亮显示目录下Extras/Editors中有各种编辑器的支持脚本,运行相应的即可。

1.模拟按键

1)“运行”

#NoTrayIcon

Send,#r

2)复制/粘贴

F6::^c

F7::^v

2.快捷键运行程序

#n::run notepad

#j::Run www.jandan.net

;按下“Ctrl + Alt + Shift + Win + Q”(这个快捷键真牛×。- -b)来启动 QQ 

^!+#q::run D:/qq/qq.exe

2)自动登录邮箱(浏览器输入法要为英文输入法)

#1::

run, http://www.126.com

sleep, 5000 ;保险起见,再等5秒(视网速)

send, {Backspace 10}用户名{tab}密码{enter} ;删除已保存的用户名,模拟键入用户名、密码、回车

return



网友ttzm33说“按十次删除键太呆板”,修改如下

#1::

run, http://www.126.com

sleep, 5000    ;保险起见,再等5秒(视网速)

send, +{home}

send, 用户名{tab}密码{enter} ;删除已保存的用户名,模拟键入用户名、密码、回车

return

3)快捷键新建文件夹,按快捷键时不要选中文件

^n::

; 第一行增加快捷键       Ctrl+N

Click right

Send, wf

Sleep, 125

Send,{Enter}

return

②以当前日期新建文件夹

^n::

; 第一行增加快捷键

Click right

Send, wf

Sleep, 125

; 把暂停时间改小

clipboard = %A_YYYY%-%A_MM%-%A_DD%

; 增加上面这句,把当前的系统日期发送到剪贴板

Send, ^v{Enter}

; 发送 Ctrl + v 和回车

return

4)打开/激活记事本

^J::

IfWinExist 无标题 - 记事本

{

        WinActivate

}

else

{

        Run Notepad

        WinWait 无标题 - 记事本

        WinActivate

}

3.快速输入网址,用户名,邮箱

;输入 /mail 然后再按下回车或空格,gmail@gmail.com 就会上屏啦

::/mail::gmail@gmail.com

;粘贴中文

::/gs::

clipboard = 煎蛋娱乐有限公司

Send ^v

return

更强的输入辅助脚本Texter.ahk

4.键盘控制声音

F10::SoundSet, +1,, mute

F11::

SoundSet +10, MASTER

SoundSet, +10, WAVE

Return

F12::

SoundSet -10, MASTER

SoundSet, -10,WAVE

Return

5.“发送到”添加“复制文件路径”

代码

#singleinstance force 

IfLess, 0, 1 

If A_IsCompiled 



               SendToDir = %UserProfile%/SendTo 

               IfNotExist, %SendToDir% 

               { 

                  MsgBox, 16,, %SendToDir% not found 

                  ExitApp 

               }    

               StringTrimRight, ScriptName, A_ScriptName, 4 

               FileCreateShortcut, %A_ScriptFullPath%, %SendToDir%/%ScriptName%.lnk 

                  , %A_ScriptDir%,,Send to Clipboard as Name,,%A_ScriptFileFullPath% 

               MsgBox, 64,, Shortcut %SendToDir%/%ScriptName%.lnk generated 

               ExitApp 



else 



               MsgBox, 16,, Compile me. 

               ExitApp 



ClipBoard= 

Loop, %0% ; For each parameter: 



               IfGreater, A_Index, 1 

                  clipboard = %clipboard%`n 

               clipboard := clipboard %A_Index% 



CoordMode, ToolTip, Screen 

ToolTip, %clipboard% -> ClipBoard,0,0 

Sleep, 2000

该代码得编译后才能运行,只要exe文件就可以了,exe文件可任意放置,只要快捷方式的目标指向程序文件即可。

用Ahk2Exe.exe编译“复制文件路径.ahk”为“复制文件路径.exe”

编译过程中自动在“C:/Documents and Settings/用户名SendTo”文件夹生成快捷方式

例子:快捷方式的目标"C:/Documents and Settings/nw/My Documents/ahk教程/脚本/复制文件路径.exe"

6.窗口置顶

START:

MouseGetPos,,,vindu,control

WinGet,vindustatus,ExStyle,ahk_id %vindu%

Transform,resultat,BitAnd,%vindustatus%,0x8

If resultat <> 0

             ToolTip,^

Else

             ToolTip,_

Sleep,50

Goto,START

LButton::

MouseGetPos,,,vindu,control

WinSet,AlwaysOnTop,Toggle,ahk_id %vindu%

If resultat = 0

{

             WinActivate,ahk_id %vindu%

             ToolTip,^

             Sleep,1000

}

Else

{

             ToolTip,_

             Sleep,1000  

             WinMinimize,ahk_id %vindu%

}

ExitApp

Esc::

RButton::

ToolTip,?

Sleep,1000

ExitApp

7.窗口收缩为标题栏

;快捷键Win+r

#r::WinRollup()

WinRollup()

{

If Not hWnd:=WinExist("A")

Return

SysGet, nT,             4

SysGet, nM, 15

VarSetCapacity(rt, 16, 0)

DllCall("GetWindowRect", "Uint", hWnd, "Uint", &rt)

nW := NumGet(rt, 8, "int") - NumGet(rt, 0, "int")

nH := NumGet(rt,12, "int") - NumGet(rt, 4, "int")

DllCall("ScreenToClient", "Uint", hWnd, "Uint", &rt)

nC :=-NumGet(rt, 4, "int")

nC -= nC<nT+nM ? 0 : nM

DllCall("GetWindowRgnBox", "Uint", hWnd, "Uint", &rt)

hRgn := DllCall("CreateRectRgn", "int", 0, "int",             0, "int", nW, "int", nC)

cRgn := DllCall("CreateRectRgn", "int", 0, "int", nC, "int", nW, "int", nH)

If Not DllCall("GetWindowRgn", "Uint", hWnd, "Uint", hRgn) || DllCall("CombineRgn", "Uint", hRgn, "Uint", hRgn, "Uint", cRgn, "int", NumGet(rt,12,"int")=nC ? 2 : 4)

             Ret := DllCall("SetWindowRgn", "Uint", hWnd, "Uint", hRgn, "int", True)

If Not Ret

             DllCall("DeleteObject", "Uint", hRgn)

DllCall("DeleteObject", "Uint", cRgn)

}

8.关闭当前窗口

;快捷键 Ctrl+`            `为Esc下方的那个键

^`::

WinGetActiveTitle, Title

WinClose, %Title%

return

;快捷键 按住鼠标左键不放再按Esc ~在这里是指示原有的左钮仍要处理,若不加~则左钮就整个失效了

~LButton & Escape::

WinGetActiveTitle, Title

WinClose, %Title%

return

;快捷键 按住左键不放再按右钮(RButton)来执行,以省去键盘的操作

~LButton & RButton::

WinGetActiveTitle, Title

WinClose, %Title%

return

连按两个Escape的写法就复杂许多 :

· 用KeyWait读取键盘输入,如果0.5秒内不是按Escape则结束 

· 0.5秒内按了Escape,则再读第二个按键,若为Escape则关闭窗口 

· 若第二个按键不为Escape则结束

~Esc::

Keywait, Escape, , t0.5

if errorlevel = 1

return

else

Keywait, Escape, d, t0.1

if errorlevel = 0

{

WinGetActiveTitle, Title

WinClose, %Title%

return

}

return 



9.打开/关闭光驱

两段代码要分开保存

;打开

list=%1%

If 0=0

             DriveGet,list,List,CDROM

Loop,Parse,list

             Drive,Eject,%A_LoopField%:

;关闭

list=%1%

If 0=0

             DriveGet,list,List,CDROM

Loop,Parse,list

             Drive,Eject,%A_LoopField%:,1

2)光驱盘符G: 快捷键Ctrl+Shift+z弹出

;Open/Close CD Tray

^+z::

DTray := !DTray

DriveGet, DStatus, StatusCD, G:

If DStatus In stopped,seeking,paused,playing

DTray = 1

DStatus =

If DTray = 1

{

Drive, Eject,G:

SetTimer, AutoCloseDTray, 15000

Return

}

If DTray = 0

{

SetTimer, AutoCloseDTray, Off

Drive, Eject, G:, 1

}

Return

;Close CD Tray After 15 Seconds

AutoCloseDTray:

SetTimer, AutoCloseDTray, Off

Drive, Eject, G:, 1

DTray =

Return

10.注册表自动定位

#NoEnv

SendMode Input

#NoTrayIcon

#SingleInstance ignore

; 注册表的HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Applets/Regedit

; 下的LastKey项保存了上一次浏览的注册表项位置,所以在打开注册表编辑器前修改它就行了

InputBox, NewLastKey, 注册表自动定位工具, 请输入要定位到的路径, , 800, 120 

If(ErrorLevel = 1)

           ExitApp

IfWinExist, 注册表编辑器 ahk_class RegEdit_RegEdit

{

           WinClose

           WinWaitClose

}

RegWrite, REG_SZ, HKEY_CURRENT_USER, Software/Microsoft/Windows/CurrentVersion/Applets/Regedit, LastKey, %NewLastKey%

Run, regedit.exe

11.隐藏/显示任务栏

1)关于隐藏任务栏,AHK的帮助文档中有个实例

WinHide ahk_class Shell_TrayWnd          ;隐藏任务栏

WinShow ahk_class Shell_TrayWnd         ;显示任务栏

2)Goyyah 找到了一个隐藏任务栏按钮的方法:利用DllCall() 来调用User32.dll 中一个未曾在官方文档中解释的函数GetTaskmanWindow,即可轻松实现:

WinHide, % "ahk_id " DllCall("GetTaskmanWindow")           ;隐藏任务栏按钮

WinShow, % "ahk_id " DllCall("GetTaskmanWindow")           ;显示任务栏按钮

12.下载文件

URLDownloadToFile, URL, Filename

13.鼠标用键盘方向键移动指定像素

例子为1像素

Left::    MouseMove, -1,    0,, R

Up::      MouseMove,    0, -1,, R

Right:: MouseMove,    1,    0,, R

Down::    MouseMove,    0,    1,, R



14.回收站改名

Gui, Show, x159 y108 h144 w477, New GUI Window

Gui, Add, Edit, x216 y30 w220 h20 vMyEdit,

Gui, Add, Text, x6 y30 w210 h20 , Enter a name to replace the old one>>

Gui, Add, Button, x26 y80 w190 h20 gRRBN, Replace Recycling Bins Name

Gui, Add, Button, x226 y80 w190 h20 gRMYN, Replace My Documents Name

Gui, Add, Button, x26 y100 w190 h20 gRRBN2D, Restor to defalt

Gui, Add, Button, x226 y100 w190 h20 gRMDN2D, Restor to defalt

; Generated using SmartGUI Creator 4.0

Return

RRBN:

GuiControlGet, w00tvar,, MyEdit

RegWrite, REG_SZ, HKEY_CURRENT_USER, Software/microsoft/windows/CurrentVersion/Explorer/CLSID/{645FF040-5081-101B-9F08-00AA002F954E}, ,%w00tvar%

return

RMYN:

GuiControlGet, w00tvar,, MyEdit

RegWrite, REG_SZ, HKEY_CURRENT_USER, Software/Microsoft/Windows/CurrentVersion/Explorer/CLSID/{450D8FBA-AD25-11D0-98A8-0800361B1103}, ,%w00tvar%

return

RRBN2D:

RegDelete, HKEY_CURRENT_USER, Software/microsoft/windows/CurrentVersion/Explorer/CLSID/{645FF040-5081-101B-9F08-00AA002F954E}, AHK_DEFAULT

return

RMDN2D:

RegDelete, HKEY_CURRENT_USER, Software/microsoft/windows/CurrentVersion/Explorer/CLSID/{450D8FBA-AD25-11D0-98A8-0800361B1103}, AHK_DEFAULT

return

GuiClose:

ExitApp

15.关闭显示器

;#NoTrayIcon

#SingleInstance force

#s::

KeyWait Control ; Wait for the key to be released. Use one KeyWait for each of the hotkey's modifiers.

KeyWait Alt

KeyWait LWin

BlockInput On

SendMessage, 0x112, 0xF170, 2,, Program Manager ; Turn Monitor Off

BlockInput Off

return

16.显示进程的路径

OnExit, HandleExit

success := DllCall( "advapi32.dll/LookupPrivilegeValueA"

                  , "uint", 0

                  , "str", "SeDebugPrivilege"

                  , "int64*", luid_SeDebugPrivilege )

if ( ReportError( ErrorLevel or !success

            , "LookupPrivilegeValue: SeDebugPrivilege"

            , "success = " success ) )

   ExitApp

Process, Exist

pid_this := ErrorLevel

hp_this := DllCall( "OpenProcess"

                  , "uint", 0x400                                 ; PROCESS_QUERY_INFORMATION

                  , "int", false

                  , "uint", pid_this )

if ( ReportError( ErrorLevel or hp_this = 0

            , "OpenProcess: pid_this"

            , "hp_this = " hp_this ) )

   ExitApp

success := DllCall( "advapi32.dll/OpenProcessToken"

                  , "uint", hp_this

                  , "uint", 0x20                                 ; TOKEN_ADJUST_PRIVILEGES

                  , "uint*", ht_this )

if ( ReportError( ErrorLevel or !success

            , "OpenProcessToken: hp_this"

            , "success = " success ) )

   ExitApp

VarSetCapacity( token_info, 4+( 8+4 ), 0 )

   EncodeInteger( 1, 4, &token_info, 0 )

   EncodeInteger( luid_SeDebugPrivilege, 8, &token_info, 4 )

      EncodeInteger( 2, 4, &token_info, 12 )                           ; SE_PRIVILEGE_ENABLED

success := DllCall( "advapi32.dll/AdjustTokenPrivileges"

                  , "uint", ht_this

                  , "int", false

                  , "uint", &token_info

                  , "uint", 0

                  , "uint", 0

                  , "uint", 0 )

if ( ReportError( ErrorLevel or !success

            , "AdjustTokenPrivileges: ht_this; SeDebugPrivilege ~ SE_PRIVILEGE_ENABLED"

            , "success = " success ) )

   ExitApp

Gui, Add, ListView, x5 y5 w800 h400, PID|file name|command line

Gui, Show, x50 y50 w810 h410, EnumProcesses experiment

total := EnumProcesses( pid_list )

loop, parse, pid_list, |

   LV_Add( "", A_LoopField, GetModuleFileNameEx( A_LoopField ), GetRemoteCommandLine( A_LoopField ) )

LV_ModifyCol( 1, "Integer Sort AutoHdr" )

LV_ModifyCol( 2, "AutoHdr" )

LV_ModifyCol( 3, "AutoHdr" )

return

HandleExit:

   DllCall( "CloseHandle", "uint", ht_this )

   DllCall( "CloseHandle", "uint", hp_this )

ExitApp

F12::Reload

GuiClose:

ExitApp

EncodeInteger( p_value, p_size, p_address, p_offset )

{

   loop, %p_size%

      DllCall( "RtlFillMemory", "uint", p_address+p_offset+A_Index-1, "uint", 1, "uchar", p_value >> ( 8*( A_Index-1 ) ) )

}

ReportError( p_condition, p_title, p_extra )

{

   if p_condition

      MsgBox,

         ( LTrim

            [Error] %p_title%

            EL = %ErrorLevel%, LE = %A_LastError%

            

            %p_extra%

         )

   

   return, p_condition

}

EnumProcesses( byref r_pid_list )

{

   if A_OSVersion in WIN_95,WIN_98,WIN_ME

   {

      MsgBox, This Windows version (%A_OSVersion%) is not supported.

      return, false

   }

   

   pid_list_size := 4*1000

   VarSetCapacity( pid_list, pid_list_size )

   

   status := DllCall( "psapi.dll/EnumProcesses", "uint", &pid_list, "uint", pid_list_size, "uint*", pid_list_actual )

   if ( ErrorLevel or !status )

      return, false

   total := pid_list_actual//4

   r_pid_list=

   address := &pid_list

   loop, %total%

   {

      r_pid_list := r_pid_list "|" ( *( address )+( *( address+1 ) << 8 )+( *( address+2 ) << 16 )+( *( address+3 ) << 24 ) )

      address += 4

   }

   

   StringTrimLeft, r_pid_list, r_pid_list, 1

   

   return, total

}

GetModuleFileNameEx( p_pid )

{

   if A_OSVersion in WIN_95,WIN_98,WIN_ME

   {

      MsgBox, This Windows version (%A_OSVersion%) is not supported.

      return

   }

   h_process := DllCall( "OpenProcess", "uint", 0x10|0x400, "int", false, "uint", p_pid )

   if ( ErrorLevel or h_process = 0 )

      return

   

   name_size = 255

   VarSetCapacity( name, name_size )

   

   result := DllCall( "psapi.dll/GetModuleFileNameExA", "uint", h_process, "uint", 0, "str", name, "uint", name_size )

   

   DllCall( "CloseHandle", h_process )

   

   return, name

}

GetRemoteCommandLine( p_pid_target )

{

   hp_target := DllCall( "OpenProcess"

                     , "uint", 0x10                              ; PROCESS_VM_READ

                     , "int", false

                     , "uint", p_pid_target )

   if ( ErrorLevel or hp_target = 0 )

   {

      result = < error: OpenProcess > EL = %ErrorLevel%, LE = %A_LastError%, hp_target = %hp_target%

      Gosub, return

   }

   hm_kernel32 := DllCall( "GetModuleHandle", "str", "kernel32.dll" )

   pGetCommandLineA := DllCall( "GetProcAddress", "uint", hm_kernel32, "str", "GetCommandLineA" )

   buffer_size = 6

   VarSetCapacity( buffer, buffer_size )

   success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", pGetCommandLineA, "uint", &buffer, "uint", buffer_size, "uint", 0 )

   if ( ErrorLevel or !success )

   {

      result = < error: ReadProcessMemory 1 > EL = %ErrorLevel%, LE = %A_LastError%, success = %success%

      Gosub, return

   }

   loop, 4

      ppCommandLine += ( ( *( &buffer+A_Index ) ) << ( 8*( A_Index-1 ) ) )

   

   buffer_size = 4

   VarSetCapacity( buffer, buffer_size, 0 )

   success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", ppCommandLine, "uint", &buffer, "uint", buffer_size, "uint", 0 )

   if ( ErrorLevel or !success )

   {

      result = < error: ReadProcessMemory 2 > EL = %ErrorLevel%, LE = %A_LastError%, success = %success%

      Gosub, return

   }

   loop, 4

      pCommandLine += ( ( *( &buffer+A_Index-1 ) ) << ( 8*( A_Index-1 ) ) )

   buffer_size = 32768

   VarSetCapacity( result, buffer_size, 1 )

   success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", pCommandLine, "uint", &result, "uint", buffer_size, "uint", 0 )

   if ( !success )

   {

      loop, %buffer_size%

      {

         success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", pCommandLine+A_Index-1, "uint", &result, "uint", 1, "uint", 0 )

         

         if ( !success or Asc( result ) = 0 )

         {

            buffer_size := A_Index

            break

         }

      }

      success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", pCommandLine, "uint", &result, "uint", buffer_size, "uint", 0 )

      if ( ErrorLevel or !success )

      {

         result = < error: ReadProcessMemory 3 > EL = %ErrorLevel%, LE = %A_LastError%, success = %success%

         Gosub, return

      }

   }

return:

   DllCall( "CloseHandle", "uint", hp_target )

   

   return, result

}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值