How can I lock my computer with AutoHotkey? - window

I'm trying to bind "Esc" key to lock my computer with AutoHotkey.
Manually pressing Winkey + l will lock my computer, but it doesn't work in my AutoHotkey script.
esc::
MsgBox Going to lock
Send, #l
Return
I have tried multiple other AutoHotkey syntax (without the modifier for example) without success.

Per the recommendation in the comments by wOxxOm:
Esc::
{
DllCall("LockWorkStation")
}
return

What you are doing in that code, is pressing the windows key first and then the 'l' key. Not both at the same time. To make key combinations, you need to press the combination key down and then the key you want to combine it with. Remember to release the key afterwards. Your code would then look like:
Send {LWin down}
Send l
Send {LWin up}
or
Send {LWin down}l{LWin up}

Just improving the code of my fellow camarades, you can block AND turn off the screen if you want to:
#J::
KeyWait LWin
KeyWait J
DllCall("LockWorkStation")
SendMessage,0x112,0xF170,2,,Program Manager
Return

Related

Hotkey wont work if its modifier is also being used as part of the trigger

This issue seems to only occur in this one program.
I have the hotkey Shift+a set in this programs hotkey editor dialog, I want to trigger it in AHK using Rshift and w;
RShift & w::
SendInput, +{a}
Return
For the life of me, it will not work. I press Rshift and w and nothing happens at all. Triggering the hotkey manually by pressing shift and a works just fine.
The strange thing is the following works:
LCtrl & w::
SendInput, +{a}
Return
So does this:
w::
SendInput, +{a}
Return
I also tried to send Rshift back up before triggering the hotkey, no luck:
RShift & w::
SendInput, {RShift up}
SendInput, +{a}
Return
Is there a rule that says you cant use the same modifier as the target hotkey in the trigger that I missed?
please dont suggest that I use other modifier keys, shift +[key] is all that I have left.
Any help would be greatly appreciated!
Firstly, you shouldn't use a custom hotkey combination when a modifier exists (unless you have a good reason to):
For standard modifier keys, normal hotkeys typically work as well or better than "custom" combinations. For example, <+s:: is recommended over LShift & s::.
And secondly you shouldn't escape keys in a Send(docs) command that don't need escaping:
Enclosing a plain ASCII letter (a-z or A-Z) in braces forces it to be sent as the corresponding virtual keycode, even if the character does not exist on the current keyboard layout. In other words, Send a produces the letter "a" while Send {a} may or may not produce "a", depending on the keyboard layout. For details, see the remarks below.
And then about the problem itself:
I of course can't know if this will work, but I'd simply try the following:
>+w::a
This would work because because the remapping syntax actually uses the blind sendmode.
Alternatively, you could, of course, just manually use the blind sendmode:
>+w::SendInput, {Blind}a

AutoHotKey: make Win+Tab act as Alt+Tab, but remap all other Win+ combinations as Ctrl+

I am trying to make my MacBook in Windows behave similar to macOS: so I can switch between apps using Win+Tab (i.e., replicate the Alt+Tab action), but have all the Ctrl+... actions (like, Ctrl+C, Ctrl+V, Ctrl+Z, etc) be accessible using the Win key (Win+C, Win+V, Win+Z).
In other words, I am trying to :
Remap Win key to Ctrl in all key combinations, but also
Have the Win+Tab act exactly as Alt+Tab (and I don't care if Ctrl+Tab stops
working as Ctrl+Tab, because I am not using that key combination at
all).
I am able to separately individually achieve 1. using LWin::Ctrl, and 2. using LWin & Tab::AltTab, but I cannot make them work together. Whenever I have something like
LWin::Ctrl
LWin & Tab::AltTab
or
LWin::Ctrl
Ctrl & Tab::AltTab
it just stops working, I am using Windows 10.
Did you try to use the symbols like documented here?
For your snippet that would mean:
LWin::Ctrl
LWin & Tab::Send, !{Tab}
There is a problem with this, as it simulates closed keystokes (regard !{Tab} as {Alt Down}{Tab}{Alt Up}. If you want to press and hold Win and then use Tab (or eventually tab multiple times), this doesn't work. To adress this issue, I found three main workarounds:
)
Let Alt stick to being pressed down:
LWin::Ctrl
LWin & Tab::Send, {Alt Down}{Tab}
LWin & Capslock::Send, {Alt Up} ;Suppose you won't use that hotkey elsewhere
)
Use something this solution by 2501:
h::AltTabMenu ; Opens the menu. Press second time to close.
n::AltTab ; Alt-Tabs through forwards
m::ShiftAltTab ; Alt-Tabs through backwards
The underlying principles/functionalities are documented here.
) Dig into AHK really deep: here (which is referenced in 1)
Annotation: Be aware that
<#::Ctrl
<#Tab::!Tab
does not work as Windows then handles the win key on its own first. You can verify This by testing:
<#::
MsgBox test
return
<#Tab::
MsgBox test
return

Remapping Alt key to Ctrl key in AutoHotKey causes both keys to be pressed

So, just like the title says, whenever I remap my LAlt key to LCtrl (or LCtrl to LAlt) they both get pressed when either key is used, and this causes a variety of issues.
The main reason I need these keys to be swapped is for use with a Mac keyboard on a Windows 7 computer. Honestly, though, it is only the beginning of my problem. I would actually like the LAlt key to be the LCtrl key, the LWin key to be the LAlt key, and the LCtrl key to be the LWin key so it appropriately emulates the Mac keyboard for personal use within Pro Tools 10. When I attempt this 3-way-swap, LCtrl and LWin function properly (as LWin and LAlt respectively), however, the LAlt key continues to press both LCtrl and LAlt anyways.
My code looks like this:
#IfWinActive
LAlt::LCtrl
LWin::LAlt
LCtrl::LWin
Return
I am fairly new to AHK, but this shouldn't be overly complicated, right? It's pretty short and sweet, and I'm not even worried about the Alt+Tab ordeal; I just need to figure out the issue so these three buttons can be swapped correctly. If anyone has any clue as to why this might be happening, I would be profoundly grateful. Thanks for your time and effort.
I think your hotkeys are probably triggering each other.
When you hit LALT, it sends LCTRL, which triggers LALT...
When you have a hotkey whose output matches the input of another hotkey, prefix your hotkeys with $ to stop them triggering each other:
$LAlt::LCtrl
$LWin::LAlt
$LCtrl::LWin
Also, I don't think your #IfWinActive is doing anything, since it has no WinTitle param, plus the return at the end is not how you end an #IfWinActive block, you start it with #IfWinActive <WinTitle> and end it with #IfWinActive

Mapping RightAlt+Ctrl+a to send Ctrl+Delete?

I have the following map:
>!+a::SendInput,+{Delete}
It's supposed to send Ctrl+Delete (delete word after cursor) when I press RightAlt+Ctrl+a but instead it's sending a Ctrl+Alt+Delete signal so it's bringing up the Windows 7 menu of shutdown, start task manager etc.
How can I send the right signal?
Appreciate any help!
The problem you are facing is that Ctrl + Alt + Delete is hard coded and is uninterruptible. There's simply no way around it, if you press that sequence, even with the Command BlockInput enabled, Windows will re-enable input and execute the command...
Try:
>!^a:: ; + symbol is Shift ^ represents Ctrl key
KeyWait, RAlt ; Waits for Right Alt to be released before Ctrl Delete is sent
SendInput,{Ctrl Down}{Delete}{Ctrl Up}
Return
An alternative although it works the exact same way:
>!^a::
While (GetKeyState("RAlt", "P"))
Continue
SendInput,{Ctrl Down}{Delete}{Ctrl Up}
Return
I'll continue to pursue other options.. at the moment I can I think of no better way to do this.

Emacs keyboard-shortcut sequences after CapsLock remapping with Authotkey

I've made simple remapping of CapsLock to Control with autohotkey:
Capslock::Control ; make Caps Lock the control button
This works OK, but in Emacs if I need to do a key sequence like Ctrl-x Ctrl-f while holding the CapsLock key continuously the whole time what gets registered is Ctrl-x f. In order to get the correct sequence I now have to release CapsLock between the keystrokes like Ctrl-x <release> Ctrl-f which is very inconvenient.
Is there a way to do the rebinding so that holding CapsLock down will work exactly as holding down the Control button?
I've also tried the following snippet without success:
#IfWinActive ahk_class Emacs
{
CapsLock::
Sendinput {Ctrl Down}
KeyWait, CapsLock
Sendinput {Ctrl Up}
return
}
Using Send or Sendplay in the above instead of Sendinput also doesn't solve my problem.
My system:
Windows 7 32bit
AHK v1.1.13.01
Emacs 24.3
Thank you!
I'm not sure why it's not working correctly for you. I added your remapping into my existing script and it seems to work fine. Can you post more of what you have? I have the following commands at the top of mine but I wouldn't think they should affect your problem.
#Persistent
#SingleInstance, Force
The commands GetKeystate
and SetKeyDelay might be helpful.
This is what I use and it works:
*Capslock::LCtrl
The asterisk is documented in the help under Keyboard Control > Hotkeys and Hotstrings:
Wildcard: Fire the hotkey even if extra modifiers are being held down. This is often used in conjunction with remapping keys or buttons. For example:
*#c::Run Calc.exe ; Win+C, Shift+Win+C, Ctrl+Win+C, etc. will all trigger this hotkey.
*ScrollLock::Run Notepad ; Pressing Scrolllock will trigger this hotkey even when modifer key(s) are down.
This symbol is ignored on Windows 95/98/ME.
I actually have this one too, +Caps Lock, if I really want caps lock:
<#Capslock::Capslock
Try this;
SetCapsLockState, AlwaysOff
CapsLock:: SendInput, {LCtrl Down}
Capslock Up:: SendInput, {LCtrl Up}

Resources