(摘) AutoHotkey代码收集

声明:内容源自网络,版权归原作者所有。若有侵权请在网页聊天中联系我
  1. 按键短按为原功能,长按为另一功能
$x::
    startTime := A_TickCount ;记录按钮开发始时间
    KeyWait, x, U ;等待按键抬起
    keypressDuration := A_TickCount-startTime ;计算按下和抬起的时差
    if (keypressDuration > 200) ;长按
    {
        Send > 
    }
    else ;短按
    {
         Send x 
    }

return

另一种方式

$t::
	KeyWait, t
	If (A_TimeSinceThisHotkey > 300)
		SetTimer, mainp, -1
	Else
		SendInput, % GetKeyState("CapsLock", "T") ? "T" : "t"
Return

mainp:
	Run, http://www.baidu.com
Return
  1. 浮动窗口显示资源占用信息(CPU/内存/磁盘),这是一个完整的示例
    #NoEnv
    #SingleInstance Force
    SetBatchLines -1
    global name      := "sysmeter"            ; gui name
    global version   := "v0.6"                ; version number
    global inifile   := "sysmeter.ini"        ; filename of .ini
    global showDate  := 0                     ; toggle show date, time & uptime (0 = Off | 1 = On) 
    global showPerc  := 1                     ; toggle between % and GB (0 = GB  | 1 = % )
    global aot       := 0                     ; toggle alwaysontop (0 = Off | 1 = On)
    global cgbg      := "464646"              ; gui background color
    global cpcpu     := "13a7c7"              ; progressbar color cpu
    global cpmem     := "13a7c7"              ; progressbar color ram & swp
    global cphdd     := "13a7c7"              ; progressbar color hdd
    global cpbg      := "686868"              ; progressbar background color
    ; LOAD INI SETTINGS =================================================================
    if FileExist(inifile)
    {
        IniRead, winx, % inifile, settings, winPosX
        IniRead, winy, % inifile, settings, winPosY
        IniRead, showDate, % inifile, settings, showDate
        IniRead, showPerc, % inifile, settings, showPerc
        IniRead, aot, % inifile, settings, alwaysOnTop
        IniRead, tran, % inifile, settings, transparency
        IniRead, cgbg, % inifile, colors, color_guibg
        IniRead, cpcpu, % inifile, colors, color_pgbar_cpu
        IniRead, cpmem, % inifile, colors, color_pgbar_mem
        IniRead, cphdd, % inifile, colors, color_pgbar_hdd
        IniRead, cpbg, % inifile, colors, color_pgbg
    }
    ; MENU ==============================================================================
    Menu, Tray, DeleteAll
    Menu, Tray, NoStandard
    Menu, Tray, Add, Save Settings, Menu_SaveSettings
    Menu, Tray, Add,
    Menu, Menu_color, Add, Blue, Menu_Color_Blue
    Menu, Menu_color, Add, Lime, Menu_Color_Lime
    Menu, Menu_color, Add, Red, Menu_Color_Red
    Menu, Menu_color, Add, Purple, Menu_Color_Purple
    Menu, Menu_color, Add, Mix, Menu_Color_Mix
    Menu, Tray, Add, Color Scheme, :Menu_Color
    Menu, Tray, Add,
    Menu, Tray, Add, Toggle Time / Uptime, Menu_Time
    Menu, Tray, % ((showDate = "1") ? "Check" : "Uncheck"), Toggle Time / Uptime
    Menu, Tray, Add, Toggle Percentage, Menu_Percentage
    Menu, Tray, % ((showPerc = "1") ? "Check" : "Uncheck"), Toggle Percentage
    Menu, Tray, Add,
    Menu, Tray, Add, Reset Transparency, Menu_Transparency
    Menu, Tray, Add, Toggle AlwaysOnTop, Menu_AlwaysOnTop
    Menu, Tray, % ((aot = "1") ? "Check" : "Uncheck"), Toggle AlwaysOnTop
    Menu, Tray, Add, Show/Hide, Menu_ShowHide
    Menu, Tray, Add,
    Menu, Tray, Add, Exit, Close
    Menu, Tray, Default, Show/Hide
    ; GUI MAIN ==========================================================================
    Create_Gui:
    Gui +LastFound -Caption +ToolWindow +hwndhMain
    Gui, Margin, 10, 10
    Gui, Color, % cgbg
    Gui, Font, s10 cFFFFFF bold, Agency FB
    if (showDate = 1)
    {
        Gui, Add, Text, xm ym w120 0x200 vTTIM,
        Gui, Add, Text, x+5 yp w75 0x202 vTUPT,
        Gui, Add, Text, xm y+7 w50 0x200, % "CPU"
    }
    else
    {
        Gui, Add, Text, xm ym w50 0x200, % "CPU"
    }
    Gui, Add, Text, x+5 yp w145 0x202 vTCPU,
    Gui, Add, Progress, xm y+2 w200 h6 c%cpcpu% Background%cpbg% vPCPU,
    Gui, Add, Text, xm y+7 w50 0x200, % "RAM"
    Gui, Add, Text, x+5 yp w145 0x202 vTRAM,
    Gui, Add, Progress, xm y+2 w200 h6 c%cpmem% Background%cpbg% vPRAM,
    Gui, Add, Text, xm y+7 w50 0x200, % "SWAP"
    Gui, Add, Text, x+5 yp w145 0x202 vTSWP,
    Gui, Add, Progress, xm y+2 w200 h6 c%cpmem% Background%cpbg% vPSWP,
    DriveGet, DrvLstFxd, List, FIXED
    loop, Parse, DrvLstFxd
    {
        Gui, Add, Text, xm y+7 w50 0x200, %A_Loopfield%:\
        Gui, Add, Text, x+5 yp w145 0x202 vP%A_Loopfield%RV,
        Gui, Add, Progress, xm y+2 w200 h6 c%cphdd% Background%cpbg% vT%A_Loopfield%RV,
    }
    Gui, Show, % ((winX != "") ? winX : "") ((winY != "") ? winY : "") AutoSize, % name
    WinSet, Transparent, % ((tran != "") ? tran : 200), % name
    WinSet, AlwaysOnTop, % ((aot = "1") ? "On" : "Off"), % name 
    OnMessage(0x201, "WM_LBUTTONDOWN")
    OnMessage(0x219, "WM_DEVICECHANGE")
    SetTimer, Update, 1000
    return
    ; SCRIPT ============================================================================
    Update:
        if (showDate = 1)
        {
            GuiControl,, TTIM, % A_DD ". " A_MMMM " " A_YYYY "   " A_Hour ":" A_Min ":" A_Sec
            GuiControl,, TUPT, % "UP: " GetDurationFormat(DllCall("Kernel32.dll\GetTickCount64", "UInt64") / 1000)
        }
        CPU := CPULoad()
        GuiControl,, TCPU, % CPU " %"
        GuiControl,, PCPU, % CPU
        
        GMSEx := GlobalMemoryStatusEx()
        GMSExM01 := GMSEx[1]                                   ; MemoryLoad in %
        GMSExM02 := Round(GMSEx[2] / 1024**3, 2)               ; Total Physical Memory in MB
        GMSExM03 := Round(GMSEx[3] / 1024**3, 2)               ; Available Physical Memory in MB
        GMSExM04 := Round(GMSExM02 - GMSExM03, 2)              ; Used Physical Memory in MB
        GMSExM05 := Round(GMSExM04 / GMSExM02 * 100, 2)        ; Used Physical Memory in %
        GMSExS01 := Round(GMSEx[4] / 1024**3, 2)               ; Total PageFile in MB
        GMSExS02 := Round(GMSEx[5] / 1024**3, 2)               ; Available PageFile in MB
        GMSExS03 := Round(GMSExS01 - GMSExS02, 2)              ; Used PageFile in MB
        GMSExS04 := Round(GMSExS03 / GMSExS01 * 100, 2)        ; Used PageFile in %
        GuiControl,, TRAM, % ((showPerc = "1") ? GMSExM05 " %" : GMSExM04 "/" GMSExM02 " GB")
        GuiControl,, PRAM, % GMSExM05
        GuiControl,, TSWP, % ((showPerc = "1") ? GMSExS04 " %" : GMSExS03 "/" GMSExS01 " GB")
        GuiControl,, PSWP, % GMSExS04
        
        loop, Parse, DrvLstFxd
        {
            i := A_LoopField
            DriveGet, cap%i%, Capacity, %i%:\
            DriveSpaceFree, free%i%, %i%:\
            used%i% := cap%i% - free%i%
            perc%i% := used%i% / cap%i% * 100
            GuiControl,, P%i%RV, % ((showPerc = "1") ? round(perc%i%, 2) " %" : round((used%i% / 1024), 2) "/" round((cap%i% / 1024), 2) " GB")
            GuiControl,, T%i%RV, % perc%i%
        }
    return
    Menu_SaveSettings:
        IniSettings(showDate, showPerc, cgbg, cpcpu, cpmem, cphdd, cpbg)
    return
    Menu_Color_Blue:
        cgbg  := "464646"
        cpcpu := "13a7c7"
        cpmem := "13a7c7"
        cphdd := "13a7c7"
        cpbg  := "686868"
        Gui, Destroy
        gosub Create_Gui
    return
    Menu_Color_Lime:
        cgbg  := "464646"
        cpcpu := "b7fe36"
        cpmem := "b7fe36"
        cphdd := "b7fe36"
        cpbg  := "686868"
        Gui, Destroy
        gosub Create_Gui
    return
    Menu_Color_Red:
        cgbg  := "464646"
        cpcpu := "ff4444"
        cpmem := "ff4444"
        cphdd := "ff4444"
        cpbg  := "686868"
        Gui, Destroy
        gosub Create_Gui
    return
    Menu_Color_Purple:
        cgbg  := "464646"
        cpcpu := "aa66cc"
        cpmem := "aa66cc"
        cphdd := "aa66cc"
        cpbg  := "686868"
        Gui, Destroy
        gosub Create_Gui
    return
    Menu_Color_Mix:
        cgbg  := "464646"
        cpcpu := "32cd32"
        cpmem := "ff8c00"
        cphdd := "1e90ff"
        cpbg  := "686868"
        Gui, Destroy
        gosub Create_Gui
    return
    Menu_Time:
        showdate := (showdate = "0") ? "1" : "0"
        Menu, Tray, ToggleCheck, Toggle Time / Uptime
        Gui, Destroy
        gosub Create_Gui
    return
    Menu_Percentage:
        showPerc := (showPerc = "0") ? "1" : "0"
        Menu, Tray, ToggleCheck, Toggle Percentage
    return
    Menu_Transparency:
        WinSet, Transparent, 200, % name
    return
    Menu_AlwaysOnTop:
        ToggleAlwaysOnTop()
    return
    Menu_ShowHide:
        ToggleShowHide()
    return
    ^WheelUp::GUITrans(1)
    ^WheelDown::GUITrans(0)
    ; FUNCTIONS =========================================================================
    WM_LBUTTONDOWN(wParam, lParam, msg, hwnd) ; WM_LBUTTONDOWN() by an AHK-Member
    {
        global hMain
        if (hwnd = hMain)
        {
            PostMessage, 0xA1, 2,,, % name
        }
    }
    WM_DEVICECHANGE(wParam, lParam, msg, hwnd)
    {
        global hMain
        if (wParam = 0x8000 || wParam = 0x8004)
        {
            Thread, NoTimers
            Gui, Destroy
            gosub Create_Gui
        }
    }
    GUITrans(b := 1) ; GUITrans() by jNizM
    {
        WinGet, ct, Transparent, % name
        WinSet, Transparent, % ((b = 1) ? ct + 1 : ct - 1), % name
    }
    ToggleAlwaysOnTop() ; ToggleAlwaysOnTop() by jNizM
    {
        WinGet, WS_EX_TOPMOST, ExStyle, % name
        if (WS_EX_TOPMOST & 0x8)
        {
            WinSet, AlwaysOnTop, Off, % name
            Menu, Tray, Uncheck, Toggle AlwaysOnTop
            aot := 0
        }
        else
        {
            WinSet, AlwaysOnTop, On, % name
            Menu, Tray, Check, Toggle AlwaysOnTop
            aot := 1
        }
    }
    ToggleShowHide()
    {
        WinGet, WS_VISIBLE, Style, % name
        if (WS_VISIBLE & 0x10000000)
        {
            WinHide, % name
        }
        else
        {
            WinShow, % name
            WinSet, AlwaysOnTop, Toggle, % name
            WinSet, AlwaysOnTop, Toggle, % name
        }
    }
    IniSettings(showDate, showPerc, cgbg, cpcpu, cpmem, cphdd, cpbg) ; IniSettings() by jNizM
    {
        WinGetPos, winX, winY,,, % name
        IniWrite, % "X" winX, % inifile, settings, winPosX
        IniWrite, % "Y" winY, % inifile, settings, winPosY
        IniWrite, % showDate, % inifile, settings, showDate
        IniWrite, % showPerc, % inifile, settings, showPerc
        WinGet, ct, Transparent, % name
        IniWrite, % ct, % inifile, settings, transparency
        IniWrite, % aot, % inifile, settings, alwaysontop
        IniWrite, % cgbg,  % inifile, colors, color_guibg
        IniWrite, % cpcpu, % inifile, colors, color_pgbar_cpu
        IniWrite, % cpmem, % inifile, colors, color_pgbar_mem
        IniWrite, % cphdd, % inifile, colors, color_pgbar_hdd
        IniWrite, % cpbg,  % inifile, colors, color_pgbg
    }
    GetDurationFormat(ullDuration, lpFormat := "d'd 'hh:mm:ss") ; GetDurationFormat() by jNizM
    {
        if (ullDuration < 86400)
        {
            lpFormat := SubStr(lpFormat, - 7)
        }
        VarSetCapacity(lpDurationStr, 128, 0)
        DllCall("GetDurationFormat", "UInt",  0x400
                                   , "UInt",  0
                                   , "Ptr",   0
                                   , "Int64", ullDuration * 10000000
                                   , "WStr",  lpFormat
                                   , "WStr",  lpDurationStr
                                   , "Int",   2048)
        return lpDurationStr
    }
    CPULoad() ; CPULoad() by SKAN
    {
        static PIT, PKT, PUT
        if (Pit = "")
        {
            return 0, DllCall("GetSystemTimes", "Int64P", PIT, "Int64P", PKT, "Int64P", PUT)
        }
        DllCall("GetSystemTimes", "Int64P", CIT, "Int64P", CKT, "Int64P", CUT)
        IdleTime := PIT - CIT, KernelTime := PKT - CKT, UserTime := PUT - CUT
        SystemTime := KernelTime + UserTime 
        return ((SystemTime - IdleTime) * 100) // SystemTime, PIT := CIT, PKT := CKT, PUT := CUT 
    }
    GlobalMemoryStatusEx() ; GlobalMemoryStatusEx() by jNizM
    {
        static MSEX, init := VarSetCapacity(MSEX, 64, 0) && NumPut(64, MSEX, "UInt")
        if (DllCall("GlobalMemoryStatusEx", "Ptr", &MSEX))
        {
            return { 1 : NumGet(MSEX,  4, "UInt")
                   , 2 : NumGet(MSEX,  8, "UInt64"), 3 : NumGet(MSEX, 16, "UInt64")
                   , 4 : NumGet(MSEX, 24, "UInt64"), 5 : NumGet(MSEX, 32, "UInt64") }
        }
    }
    ; EXIT ==============================================================================
    Close:
    GuiClose:
    GuiEscape:
        ExitApp
  1. 设置互斥体,阻止脚本重复运行
    /*函数名: Single
      设置互斥体,阻止脚本重复运行
      功能与#SingleInstance差不多,只是这玩样可以自定义
          另:
            经测试发现#SingleInstance只识别脚本文件名,改名可重复运行,
            在有热键的脚本里要使用 #SingleInstance,off 关闭此功能.
    参数:
      给这个脚本设置一个独一无二的识别字符串(指纹?)
    返回值: 1 为重复,0为第一次
      
    例子:
      Single("7EF47F0D-4D37-542E-5787-90FDD196D04B")
      Single("123")
    */
    Single(flag) { ;,返回1为重复,返回0为第一个运行
      DllCall("CreateMutex", "Ptr",0, "int",0, "str", "Ahk_Single_" flag)
      return A_LastError=0xB7 ? true : false
    }
    ;=========================
    ;使用案例,脚本改名都没用,照样只能运行一个.
    #SingleInstance,Off ;关闭自带互斥功能
    if (Single("456")) {  ;独一无二的字符串用于识别脚本,或者称为指纹?
      MsgBox,16,提示,程序已启动!`n请勿重复运行
      ExitApp
    }
  1. 图片尺寸
PictureSize(pic, ByRef winw, ByRef winh) {
    objImage := ComObjCreate("WIA.ImageFile")
	objImage.LoadFile(pic)
	winw := objImage.Width
	winh := objImage.Height
    return
}
  1. 透明图片
    SplashImage, Splash.png, b w60 h31,,, splash  ; 透明
    WinSet, Transparent, 250, splash    ; 透明度
    WinSet, TransColor, #0, splash  ; 透明色
    WinSet, ExStyle, +0x20, splash  ; 允许鼠标穿透
    WinSet, AlwaysOnTop, On, splash ; 置顶在最前面
  1. 桌面滚动字幕 在顶部显示一个滚动的字幕,或许能透明背景就更好了。
^+z::
    InputBox, Sid_Inputed,屏幕文字滚动, `n在桌面上显示点啥`n 快捷键:Ctrl + Shift + z ,, 285, 175,,,,,
    If ErrorLevel
        Return
    Gui, Gui_ShowTextOnDesktop: destroy
    Gui, Gui_ShowTextOnDesktop: +LastFound +AlwaysOnTop -caption +toolwindow
    ;Gui, Gui_ShowTextOnDesktop: color, red
    Gui, Gui_ShowTextOnDesktop: font, s28 cFFFFFF bold, 微软雅黑 
    txt_x:=a_screenwidth
    Gui, Gui_ShowTextOnDesktop: add, text, x%txt_x% y0 w%a_screenwidth% h50 c111111 r1 vtxt, %Sid_Inputed%
    Gui, Gui_ShowTextOnDesktop: show, x0 y0 w%a_screenwidth% h50
    settimer, ShowText, 10
return
 
ShowText:
    txt_x -= 1
    if (txt_x <= -a_screenwidth)
        txt_x:=a_screenwidth
    GuiControl, Gui_ShowTextOnDesktop: Move, txt, x%txt_x% y0
return
 
Gui_ShowTextOnDesktopGuiEscape:
    Gui, Gui_ShowTextOnDesktop: Destroy
Return
  1. 获取蓝牙设备的连接状态 搞个人走即关,人来即来的东西还错。
    ;获取蓝牙名称和状态,0表示未连接,32表示已连接
    DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
    VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
    NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
    NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint")   ; fReturnAuthenticated
    VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
    NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
    loop
    {
       If (A_Index = 1)
       {
          foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO)
          if !foundedDevice
          {
             MsgBox,没有蓝牙设备!
             return
          }
       }
       else
       {
          if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
          {
             msgbox,没找到下一个!
             break
          }
       }
       MsgBox,% "设备名称:" StrGet(&BLUETOOTH_DEVICE_INFO+64) "   连接状态:" NumGet(&BLUETOOTH_DEVICE_INFO, 20, "Int")
    }
    DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
    ExitApp
  1. 监控USB设备变化
#Persistent
OnMessage(0x219, "notify_change")
notify_change(wParam, lParam, msg, hwnd)
{
	SetTimer,checkDrive,2000
}
return
checkdrive:
	i=0
	allDrives:=""
	DriveGet, A, List,
	StringSplit,D, A,
	loop,%d0%
	{
		DRV:= % d%a_index% . ":\"
		i++
		DriveGet       , type1   , type     , %DRV%     ;- removable / fixed
		DriveGet       , cap1    , capacity , %DRV%
		DriveSpaceFree , free1   ,            %DRV%
		DriveGet       , fs1     , fs       , %DRV%     ;- FAT
		DriveGet       , label1  , label    , %DRV%     ;- volume label >\\server1
		DriveGet       , serial1 , serial   , %DRV%     ;- volume serial number
		DriveGet       , status1 , status   , %DRV%
		allDrives .= drv . "  " . type1 . "  " . free1 . "-MB`n"
	}
	VT:=D0       ;- totalDrives
	last         = %drv%
	DriveGet    , serial2 , serial   , %last%     ;- volume serial number
	if (serial2="3772709444")
	{
		SetTimer,checkDrive,off
		try
			Run,charmap
	}
	else
	{
		SetTimer,checkDrive,off
		Run,::{20d04fe0-3aea-1069-a2d8-08002b30309d}
		MsgBox, 262208,Info ,AllDrives=`n%allDrives%`n
	}
return
esc::ExitApp

相关文章