Skip to content

Instantly share code, notes, and snippets.

@sepsol
Last active December 6, 2022 01:07
Show Gist options
  • Save sepsol/00db19e3516e7e50d6850da645707878 to your computer and use it in GitHub Desktop.
Save sepsol/00db19e3516e7e50d6850da645707878 to your computer and use it in GitHub Desktop.
; AutoHotKey script
; Sepehr Soltanieh 2022-11-30
; Feel free to reuse, edit, and redistribute : https://gist.github.com/sepsol/00db19e3516e7e50d6850da645707878
; Run this script as admin on Windows startup: https://stackoverflow.com/questions/63889537/run-ahk-script-as-admin-on-startup
;=================================
; macOS style keys for Windows 11
;=================================
; Change the masking key from "ctrl" to something unassigned such as vkE8 or vkFF.
; If this is not done, "ctrl" will be pressed any time a shortcut starts with "alt" or "win" metakeys.
; Read more at https://www.autohotkey.com/docs/Hotkeys.htm#Symbols
#MenuMaskKey vkE8
; Close apps with Ctrl+Q - similar to Cmd+Q on macOS
; Remember to run this script as admin for this shortcut to take effect on all windows including the elevated ones
^q::Send !{F4}
; Keyboard shortcuts for when typing
#If inLegacyTextInput() or inWindoinVSCodeApp or inWindowsCalendar() or inStickyNotes() or inMicrosoftToDo() or inBitwarden() or inTerminal() or inVSCodeApp() or inVSCodeWeb() or inDiscord() or inBusinessTeams() or inPersonalTeams() or inTelegram() or inUnigram() or inWhatsApp() or inPhoneLink() or inSignal() or inSkype() or inFacebook() or inInstagram() or inTwitter() or inTweeten() or inCodeSandbox() or inCodePen() or inReplit() or inLeetCode() or inHackerRank() or inCodeWars() or inTwilioQuest()
; Command + Backspace/Delete deletes whole line
!BS::Send +{Home}{BackSpace}
!Del::Send +{End}{Delete}
; Move the carret to one end of line
!left::Send % isLTR() ? "{Home}" : "{End}"
!right::Send % isLTR() ? "{End}" : "{Home}"
; Select all the text from the current carret position to one end of line
!+left::Send % isLTR() ? "+{Home}" : "+{End}"
!+right::Send % isLTR() ? "+{End}" : "+{Home}"
; Move the carret to one end of the file (Disabled for VSCode compatibility)
;!up::Send ^{Home}
;!down::Send ^{End}
; Select all the text from the current carret position to one end of the file (Disabled for VSCode compatibility)
;!+up::Send ^+{Home}
;!+down::Send ^+{End}
#If ; End of If block
; Horizontal scroll in VS Code and other chromium-based apps
#IfWinActive ahk_class Chrome_WidgetWin_1
WheelLeft::Send +{WheelUp}
WheelRight::Send +{WheelDown}
#If
;=================================
; Implementation Details
;=================================
; Environments that the script will apply the shortcuts for
inWindowsMail() {
SetTitleMatchMode 2 ; Source: https://www.autohotkey.com/docs/commands/SetTitleMatchMode.htm
return WinActive("Mail ahk_class ApplicationFrameWindow")
}
inWindowsCalendar() {
SetTitleMatchMode 2 ; Source: https://www.autohotkey.com/docs/commands/SetTitleMatchMode.htm
return WinActive("Calendar ahk_class ApplicationFrameWindow")
}
inStickyNotes() {
return WinActive("Sticky Notes ahk_class ApplicationFrameWindow")
}
inMicrosoftToDo() {
return WinActive("Microsoft To Do ahk_class ApplicationFrameWindow")
}
inBitwarden() {
return WinActive("ahk_exe Bitwarden.exe ahk_class Chrome_WidgetWin_1")
}
inTerminal() {
return WinActive("ahk_exe WindowsTerminal.exe")
}
inVSCodeApp() {
return WinActive("ahk_exe Code.exe ahk_class Chrome_WidgetWin_1")
}
inVSCodeWeb() {
SetTitleMatchMode 2 ; Source: https://www.autohotkey.com/docs/commands/SetTitleMatchMode.htm
return WinActive("Visual Studio Codeahk_class Chrome_WidgetWin_1")
}
inDiscord() {
return WinActive("ahk_exe Discord.exe ahk_class Chrome_WidgetWin_1")
}
inBusinessTeams() {
return WinActive("ahk_exe Teams.exe ahk_class Chrome_WidgetWin_1")
}
inPersonalTeams() {
return WinActive("ahk_exe msteams.exe ahk_class Chrome_WidgetWin_1")
}
inTelegram() {
return WinActive("ahk_exe Telegram.exe")
}
inUnigram() {
return WinActive("Unigram ahk_class ApplicationFrameWindow")
}
inWhatsApp() {
return WinActive("WhatsApp ahk_class ApplicationFrameWindow")
}
inPhoneLink() {
return WinActive("ahk_exe PhoneExperienceHost.exe")
}
inSignal() {
return Winactive("ahk_exe Signal.exe ahk_class Chrome_WidgetWin_1")
}
inSkype() {
return WinActive("ahk_exe Skype.exe ahk_class Chrome_WidgetWin_1")
}
inFacebook() {
return WinActive("Facebook ahk_exe msedge.exe ahk_class Chrome_WidgetWin_1")
}
inInstagram() {
return WinActive("Instagram ahk_exe msedge.exe ahk_class Chrome_WidgetWin_1")
}
inTwitter() {
return WinActive("Twitter ahk_exe msedge.exe ahk_class Chrome_WidgetWin_1")
}
inTweeten() {
return WinActive("ahk_exe Tweeten.exe ahk_class Chrome_WidgetWin_1")
}
inCodeSandbox() {
return WinActive("CodeSandbox ahk_class Chrome_WidgetWin_1")
}
inCodePen() {
return WinActive("CodePen ahk_class Chrome_WidgetWin_1")
}
inReplit() {
return WinActive("Replit ahk_class Chrome_WidgetWin_1")
}
inLeetCode() {
return WinActive("LeetCode ahk_class Chrome_WidgetWin_1")
}
inHackerRank() {
return WinActive("HackerRank ahk_class Chrome_WidgetWin_1")
}
inCodeWars() {
return WinActive("Codewars ahk_class Chrome_WidgetWin_1")
}
inTwilioQuest() {
return WinActive("ahk_exe TwilioQuest.exe ahk_class Chrome_WidgetWin_1")
}
; Twitter
; Implementing the "inLegacyTextInput" function (originally named "caretViaAcc"):
; This function will help with detecting when text input is focused in Windows.
; This won't work with non-native textboxes such as those coming with chromium.
; Source: https://www.autohotkey.com/boards/viewtopic.php?t=64491
inLegacyTextInput() {
return caretViaAcc()
}
caretViaAcc() { ; based on 'caret - get information via Acc' sample subroutine by jeeswg (cf. https://www.autohotkey.com/boards/viewtopic.php?f=5&t=39615)
static OBJID_CARET := -8
static _h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
local vCtlClassNN, hCtl
ControlGetFocus, vCtlClassNN, A
ControlGet, hCtl, Hwnd,, % vCtlClassNN, A
return Acc_Location(Acc_ObjectFromWindow(hCtl, OBJID_CARET)).x
}
; relevant functions extracted from acc.ahk (cf. https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk); note: for convinience Acc is loaded @ caretViaAcc
Acc_ObjectFromWindow(hWnd, idObject:=-4) {
local IID, pacc := ""
if (DllCall("oleacc\AccessibleObjectFromWindow","Ptr",hWnd,"UInt",idObject&=0xFFFFFFFF,"Ptr",-VarSetCapacity(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"),"Ptr*",pacc)=0)
return ComObjEnwrap(9,pacc,1)
}
Acc_Location(acc, childId:=0, ByRef position:="") {
try acc.accLocation(ComObj(0x4003,&x:=0),ComObj(0x4003,&y:=0),ComObj(0x4003,&w:=0),ComObj(0x4003,&h:=0),childId)
catch
return
position := "x" NumGet(x,0,"int") " y" NumGet(y,0,"int") " w" NumGet(w,0,"int") " h" NumGet(h,0,"int")
return {x:NumGet(x,0,"int"), y:NumGet(y,0,"int"), w:NumGet(w,0,"int"), h:NumGet(h,0,"int")}
}
; Implementing the "isLTR" and "getLang" functions:
; isLTR function will determine whether the found language is RTL or not.
; getLang function will help with determining the current keyboard language of the user.
; Inspiration: https://www.autohotkey.com/board/topic/73830-detecting-the-currently-active-language/
; Windows Language Identifiers and Locales: http://msdn.microsoft.com/en-us/library/aa912040
isLTR() {
lang := getLang()
if (lang = "FAR" || lang = "ARA")
{
return false
}
else
{
return true
}
}
getLang() {
SetFormat, Integer, H ; set the int format to hexadecimal
langHex := DllCall("GetKeyboardLayout", Int,DllCall("GetWindowThreadProcessId", int,WinActive("A"), Int,0))
SetFormat, Integer, D ; set the int format back to decimal
lang := ""
if (langHex = 0x4090409)
{
lang := "ENG"
}
else if (langHex = -0xFC5FBD7)
{
lang := "FAR"
}
else
{
lang := langHex
}
return lang
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment