I want to use AutoHotKey to disable Alt+F4 when they are pressed within 0.05 seconds of each other. Otherwise, I'd like it to work as normal.
Explanation:
My Lenovo Yoga 2 Pro has alternate functions for the function keys.
For example: "F3" is mapped to volume+, "F4" is mapped to "close active window"
There are two modes:
Old-school mode: F3 just acts as F3, and you must hold Fn+F3 key to activate volume+
New-school mode: Pressing F3 activates volume+, and Fn+F3 will do the normal F3.
In either mode, I run the risk of closing my active window when I go to use volume+ because they are too close, which is very problematic. Note that AutoHotKey cannot detect the Fn key, thus I cannot use that to solve my issue.
The image below shows the AutoHotKey Key History tool. In New-school mode, I typed "asdf" and then pressed "F4" which is "close active window". You can see this actually simulates ALT+F4, and there is a very short duration between ALT and F4...
I'm thinking that I could disable this "close active window" function by having AutoHotKey interrupt an ALT+F4 combo when there is less than 0.05 seconds between the two keys. Can this be done?
Edit:
In response to Blauhirn's code, here is the original, edited for a shorter wait duration, (from 50 to 10). It works most of the time, though 1/10 times the window is still cosed:
~alt::
hotkey, alt, off
hotkey, !F4, doNothing, on
sleep, 10
hotkey, !F4, doNothing, off
while(getKeyState("alt"))
sleep, 1
hotkey, alt, on
return
doNothing:
return
Here is a change I thought would fix my focus issue by sending a 2nd Alt when the "close active window" was detected:
doNothing:
send {LAlt}
return
However, the 2nd Alt is not sent. It IS sent when the delay is above 40ish, however I find that is way too long, and in turn it interferes with my manual use of Alt+F4.
Have you tried using simply
F4::return
? Maybe this will override the Lenovo action for F4
Other than that, here are the two approaches I can think of:
Disabling the ALT+F4 standard win hotkey by default. Adding a custom hotkey for a delayed F4
!F4:: ; by default:
doNothing: ; this is a label (see GoSub)
return ; == do nothing
~alt:: ; alt was pressed
sleep, 50 ; wait 50 milliseconds
if(!getKeyState("alt")) ; if alt is NOT pressed down anymore, exit
return
else ; (else is actually unnecessary here)
hotkey, !F4, close ; Add new AltF4-hotkey
return
close:
winclose, A ; close the Active window
return
~alt up:: ; alt is being released
hotkey, !F4, doNothing ; remove the new AltF4 hotkey and go back to custom standard behaviour: do nothing.
return
it still triggers Alt, which usually leaves me in the menu of the active window (File, Edit, View, etc), or if typing within a textarea (such is this), it will remove typing focus.
well yes. If you decide to keep the lenovo keys, I don't think there is a way to prevent it. As you suggested, sending ALT again should solve the problem
using Input, after ALT has been pressed. Input blocks user input for a configurable time, as long as the V option is used.
(3. disabling your Lenovo Yoga 2 Pro special keys. If you need the F3 function, you can do that in AutoHotkey e.g. using send {volume_up}
Related
In an fps I am trying to setup a hotkey such that when I am holding Lbutton (leftmouse), primary fire of a gun, and then tap RButton the LButton ceases and RButton commences. Ingame with no script, for other weapons, I get the desired result automatically. That is: I'm holding LButton, but if I also hold RButton 'after', the LButton ceases and the RButton's burst fire does its thing. However a different scoped weapon, with no script, freezes up when the above is done.
So I'm looking to, as soon as RButton is pressed, cease all user input of LButton until RButton's hotkey finishes.
~RButton
;Blockinput Mouse ;Send/Sendmouse
Send, {Click Left}{Click Right}{Click Left}
;BlockInput off
return
You can think of the Rbutton script as for a scoped rifle. Click Left activates scope, Click right fires it. Currently the desired behaviour only works when I completely remove my hand from LButton and then press RButton....I am also able to get ~LButton & ~RButton:: to work but that only covers the case when they r both first pressed at the exact same time, I want to cover the instance where LButton is pressed and held before RButton.
Ive tried applying blockinput, like seen above, had the UAC workaround to get it working, blocks certain things but doesnt seem to block any heldkeys when it is proc'd like the lbutton unless I'm missing something. If there was a "Freeze user input of LButton as soon as RButton procs, until RButton's script finishes" that would solve the problem I would think. I tried adding sleep delays, but they dont seem to help. The issue seems to be that LButton, when held, continues to proc when RButton first activates.
I used the below while holding LButton, and the sound DOES proc, so this must be possible to script.
*RButton::
Soundbeep
return
EDIT:
;code here works but only with that 100 sleep where i have to remove my hand from pressing the lbutton within 100ms of pressing rbutton.
*RButton::
GetKeyState, state, lbutton, p
if (state = "D")
{
soundbeep
send {Click Left Up}
sleep 100
;i need to actually disable the previous keypresses from it
}
Send, {Click Right}{Click Left}{Click Right}
return
From what I understood from your question, you don't really need to block user mouse input so much as Sending a Left MouseUp command if the Left mouse button was held down. I was having difficulty understanding what the end goal you were looking for was, but I wrote this based on what I was able to understand:
#SingleInstance Force
*RButton::
Soundbeep
If (getKeyState( "lbutton", "p" )){
Click, Left, Up
Send, {Click Left}{Click Right}{Click Left}
} else {
Send, {Click Left}{Click Right}{Click Left}
}
return
esc::ExitApp
UPDATE:
Based on the clarification from the comments of the question, this should work. Understood functionality: If the right mouse button is clicked while the left mouse button is held down, release the LMB, but do not suppress the RMB input.
*~RButton::
If (getKeyState( "lbutton", "p" ))
Click, Left, Up
return
UPDATE 2:
Now releasing the LMB before the RMB is pressed. I have a feeling that whatever game that you are playing will not be able to detect zero-tick switches between keystates, so I included a Sleep function that you can fine-tune/ remove as needed.
*RButton::
If (getKeyState( "lbutton", "p" )){
Click, Left, Up
Sleep 100
}
Click, Right, Down
KeyWait RButton
Click, Right, Up
return
Example: according to the menu, the keyboard shortcut for Find Usages should be option F7.
When I use the built-in keyboard, and actually press fn + Shift + 7 ... it works as expected.
When I use my external Magic 2 Apple keyboard, and press option + F7 ... nothing happens?!
What am I doing wrong?
Update: I just figured that my external keyboard has a fn key as well. So when I press fn + option + F7 that works. But that is super cumbersome (the fn key is to the right of backspace, it is hard to press that together with any function key).
Change the behavior of function keys on your Mac document is probably what you are looking for:
If you prefer the top row of keys to always behave as standard
function keys without holding the Fn key:
Choose System Preferences from the Apple menu.
Click Keyboard.
Click the Keyboard tab if it's not already highlighted.
Select "Use all F1, F2, etc. keys as standard function keys"
In order to end my custom keyboard mapping for coding, I have placed the following line at the bottom of my script:
Esc::ExitApp
However, a single keystroke (especially the Esc) happens too much for other reasons. Therefore, I would prefer a sequence of single keystrokes (not holding down any key) to exit the Autohotkey mapping, such as "Esc and then LCtrl" or "Esc, LCtrl and then again Esc"
I have tried the idea from AutoHotKey key SEQUENCE, not just single-key hotkey :
Esc::
Input Key, L1
if Key=LCtrl
ExitApp
return
But it doesn't seem to do the trick: on pressing Esc and then LCtrl, the mapping doesn't stop. (Also, if I press Esc and then another key (not LCtrl) that next keystroke is ignored, something I would preferably not have.
Version 1a:
LCtrl & Esc::
ExitApp
return
You can add this code anywhere in your script. Press LCtrl and after Esc for script to exit.
Version 1b:
Esc & LCtrl::
ExitApp
return
You can add this code anywhere in your script. Press Esc and after LCtrl for script to exit.
You can add both Version 1a and Version 1b. In that case, when you press LCtrl and after Esc OR Esc and after LCtrl script will exit.
Version 2 (it uses different method, use it if for some reason you don't like Version 1a or/and Version 1b):
Loop
{
a := GetKeyState("Esc")
if (a=1)
{
b := GetKeyState("LCtrl")
if (b=1)
{
ExitApp
}
}
}
That code should be launch after your other code will remap all keys you need. Press LCtrl and after Esc OR Esc and after LCtrl for script to exit.
EDIT:
Version 3:
Loop
{
a := GetKeyState("Esc")
if (a=1)
{
Loop,60 ;number of 50 milliseconds script will wait to CTRL press. Example 60 means 60*50=3000, so script will wait 3000 milliseconds (1sec=1000 milliseconds) for CTRL press. After that time you have to press Esc again.
{
b := GetKeyState("LCtrl")
if (b=1)
{
ExitApp
}
Sleep, 50
}
}
}
That code should be launch after your other code will remap all keys you need. Press Esc (now you can release Esc ) and within 3000 milliseconds (1sec=1000 milliseconds) press LCtrl. After that time you have to press Esc again for script to exit. If you want to modify time after which you need to press Esc again read the comment in the code.
Also, always use AutoHotkey and its documenatation from http://ahkscript.org/ (current uptodate version, new official website)! AutoHotkey and its documentation from autohotkey.com is outdated and you may have some problems using them!
Following is the Vimperator help about the insert mode
I have two questions in this regard
How the key combination i_i should be typed? pressing i key once brings me to ignore mode and twice pulls me out and no trace of Insert Mode.
Also I want to know what are the second series of keyboard combinations which are recomended at the end of each line such as i_< C-] for < C-i>. They show up in gray in oppose to the main shortcuts recommended in magenta.
<C-i> is the key combination. i_<C-i> simply means <C-i> while in insert mode. This is shown in the docs to make it easy to find what a particular key combination does in a given mode.
You can only enter insert mode if you are focused on a text area. Normally when you focus on a text area you are immediately put into insert mode. However, if you use :set noinsertmode then when you focus on a text area you will not initially be insert mode. You can then enter insert mode by pressing i.
If you are not focused on a text area then pressing i will take you in and out of ignore mode.
:help buffer
then you will find [count]gi is what you need.
Actually, press C-t to shift from insert mode to textArea mode, or vice versa, press i. as you know, i_i means press i in insert mode. So, textArea mode can be considered as a sub-mode of insert mode.
I have a small task I would like to automate with Autohotkey and it looks like it is more or less directly transferable to autohotkey syntax:
1. Ctrl+v
2. Alt+tab
3. Click certain link in a window (no combo-key for this but it's always in the same place)
4. Enter (carriage return)
5. Alt+s
6. Ctrl+v
7. Enter
Now it would be nice to map this combo to something else e.g. Windows Key+Space.
What I have got so far is:
0. SetWinDelay 100 (using a connection to an remote computer)
0. SetKeyDelay 0
1. Send, ^c
1. ClipWait, 0.1
2. Send, {Alt down}{tab}
2. Send, {Alt up}
3. ?????
4. Send, {enter}
5. Send, !s
6. Send, ^v
7. Send, {enter}
Is this approximately right? Anyone up for helping me fix it or filling in the holes, so to speak :)
Another alternative to step 3, 4 and 6 would be to simply loop though the contents of the clipboard (a number string) and sending each letter of the string to keypresses? Maybe this would be the easier way
If you want to "click" on a certain position, to open a menu, you can first right click on your AutoHotKey icon and open the "window spy". This window spy will show you the mouse position. Yo can use the mouse positions to perform your actions in the active application.
Example:
SoundBeep 1000, 300 ; Wake up user
SplashTextOn, 200, 100, Script Preparations, Please Click on the person icon link. ; Show new Instructions text
WinMove, Script Preparations,, (A_ScreenWidth/2)+150, (A_ScreenHeight/2)+200 ; Move the window with the name "Script Preparations" Down and Right on the main screen
KeyWait, LButton, D ; Wait for LeftMouseButton click Down
MouseGetPos, xposE ,yposE ; Store the position where the mouse was clicked (Employee)
MouseClick, left, %xposE% ,%yposE%, 2 ; Perform a double mouse click on the captured mouse location
SplashTextOff ; Remove Text box
In this case, I first ask the user to manually click on the right location. This is only required when the position to click changes WITHIN the active window (variable tiles within the active window). Once you have the position stored, you can re-use it all throughout your script.
b.t.w. instead of using Alt+Tab, I suggest using this:
settitlematchmode, 1 ; Set search in title to start with....
settitlematchmode, Fast ; Slow is not required here. Slow is only required when hidden text needs to be found.
SwitchWindow("Microsoft Excel - 1 QRM Upload and Change Template") ; Activate the
window with the title: Microsoft Excel - 1 QRM Upload and Change Template
You could even use someting like this:
SetTitleMatchMode, 2 ; Ensure that the Title Match mode is set to 2: Find anywhere in the title
SetTitleMatchMode, Fast ; Ensure that the Title Match mode is set to FAST
winactivate, %WindowName% ; Activate the window with the title stored in the variable WindowName
WinWaitActive, %WindowName%, , 5 ; Wait up to five seconds for the screen
if ErrorLevel ; Execute this when the window is not activated within 5 seconds
{ ; Start-If Wait failed
SoundBeep 1000 , 1000 ; Warn the user
MsgBox,4097,Time Out, Script timed out while waiting for %WindowName%.`n`rYou Must manually activate %WindowName% and then continue the script by pressing OK. ; Message to user
IfMsgBox, Cancel ; Do when the user clicked on Cancel
{ ; Start-If User clicked Cancel
ExitApp ; Exit this program when the user clicked on Cancel
} ; End-If User clicked Cancel
WinWaitActive, %WindowName%, , 5 ; Try to activate the window AGAIN
if ErrorLevel ; If window can't be found
{ ; Start-If window can't be found
MsgBox,4096,Exit, %WindowName% still NOT Active. ; Warn user
ExitApp ; Exit this program when the expected window is still not found
} ; End-If window can't be found
} ; End-If Wait failed
Regards,
Robert Ilbrink