autohotkey long text and in a virtual machine - autohotkey

So I'm trying to learn autohotkey scripts and the documentation is lacking at best. First, can authotkey read commands and perform actions and such inside a virtual machine? I have a windows host and a linux virtual machine running eclipse. I'd like to get a hostring (or a keyboard macro, either is fine) to put in some long (10+ lines) of text. Can that actually work in a VM or do I have to run autohotkey inside the VM for it to work?
As for implementing this, I have 2 problems. First, how do I display multiple lines of text from a keyboard macro? I know about the Send command, but I haven't figured out how that works. I have this:
:*:insert::
(
Text to
insert
goes here
and more here
)
And this works fine except in notepad++, it inserts consecutively more tabs, so it will look like
Text to
insert
goes here
and more goes here
And so in my many line macro, by the end it's several pages scrolled off the screen.
As for keyboard macro, changing the above to
#c::
Send{Raw} (
stuf
to send
)
Return
This gives syntax errors and I have no idea what the correct way of doing that would be. Should I just stick with using hotstrings?

You could try to modify the clipboard and use control + v to paste it into the proper place.
Try:
#c::
{
clipboard := "yourtext`nMultiline`nYet another line"
send, {control down}v{control up}
return
}

The first 'insert' hotstring is correct,
however, you would get the same result that you describe,
if you performed manually, the keypresses that the hotstring is sending.
To get the output you want,
you need to change these two settings:
Settings, Preferences...,
Auto-Completion,
untick: Enable auto-completion on each input
Settings, Preferences...,
MISC.,
untick: Auto-indent
the '#c' hotstring is amended below:
#c::
Send {Raw}
(
stuf
to send
)
Return

Related

WinActivate does not work as expected. Re-activating focus to the starting window

I am having some serious struggles fully grasping the control on activating windows and forcing their focus and foremost position.
In order to debug a larger script I made a separate script to test the use of WinActivate and again I am observing frustrating behaviour as it either all together ignores the title I have defined or is failing in some other way. In the smaller test script I am simply requesting that the window in which the hotkey was triggered be set as active after another action, specifically an input box
Below is the simple code for testing:
F10::
SetTitleMatchMode, 1
DetectHiddenWindows, Off
WinGetTitle, startTitle, A
msgbox % "Start Title = <" . startTitle . ">"
;WinActivate, startTitle
inputbox, mode, Test box, Testing,,260,160
sleep 500
WinActivate, startTitle
Return
This code does not properly activate the starting window. For example I execute the hotkey in an empty notepad window and upon submitting blank into the input box the focus becomes notepad++ on my second monitor. The second time I press the hotkey from within notepad (or another application) notepad does not lose focus. In a third execution I begin from notepad again and after the input box appears I switch the focus to another window. I again submit blank to the inputbox but that new window remains the focus and notepad is not activated or brought to the foremost position.
Can someone please explain to me what is going on with WinActivate?
I was having similar frustration with unexpected results making a windows script host file and I think I must be missing some fundamental detail in windows.
You are trying to activate a window that start with the literal text "startTitle".
You forgot(?) to either enter expression syntax with % or use the legacy way of referring to a variable %startTitle% (please don't use legacy).
Extra stuff:
You shouldn't specify SetTitleMatchMode and DetectHiddenWindows inside your hotkey statement. There is no need (unless there actually is) to set those every time you hit the hotkey. Just specify them at the top of your script once.
Both of them are useless for you though, below I'll show why. Also DetectHiddenWindows is already off by default.
WinGetTitle is not good to use for this. What you actually want to do is get the hwnd of the window you wish by using e.g. WinExist().
And then refer to the window by its hwnd. Much better than working with window titles, and impossible to match the wrong window as well. To refer to a window by its hwnd, you specify ahk_id followed by the hwnd on a WinTitle parameter.
And lastly, the concatenation operator . is redundant. Of course you may prefer to use it, but in case you didn't know, it can just be left out.
Here's your revised code:
F10::
_HWND := WinExist("A")
MsgBox, % "Start hwnd = <" _HWND ">"
InputBox, mode, Test box, Testing,,260,160
Sleep, 500
WinActivate, % "ahk_id " _HWND
Return

How do I force Windows to ignore an AutoHotKey and instead pass that hotkey directly to the active window?

My cPanel has a text editor that allows multi-cursor functionality using Ctrl-Alt-Up, Ctrl-Alt-Down, Ctrl-Alt-Right, and Ctrl-Alt-Left. Unfortunately, Windows has default hotkeys for these key combinations that rotate the display on your screen. When I try to use these key combinations in the text editor, Windows hijacks them before they can get to the active window.
I first searched to see if there's any reasonably easy way to turn off specific Windows default hotkeys. My search only turned up results that turn off all hotkeys. I then decided to download AutoHotKeys and see if I could write a script to achieve what I was looking to do. Below are some examples; I'll stick with Ctrl-Alt-Right just to select one out of the four:
First block of code is the same in all 4 attempts:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2 ;this and the next line should only perform the scripted
#IfWinActive Opera ;hotkey if the active window's title contains 'Opera'
Attempt 1: Hotkey correctly bypasses Windows default display-rotating action, and sends the keystrokes to the text editor in Opera. However, the text editor enters the string "ight" wherever the cursor is. There is no Ctrl-Alt-R hotkey in the text editor, and it must not be a Windows default hotkey either. So it sends Ctrl-Alt-R to Opera, which does nothing. Then it sends "ight" which is typed out. (This is the "most successful" of the 4 attempts, but definitely does not achieve the desired outcome):
^!Right::
Send, ^!Right
return
Attempt 2: Only differs from #1 with SendPlay instead of Send. The Hotkey again correctly bypasses Windows default display-rotating action. However, the text editor does not appear to do anything at all:
^!Right::
SendPlay, ^!Right
return
Attempt 3: Back to just Send, but now with curly braces around the second "Right". Now the hotkey doesn't even bypass Windows default display-rotating action (this truly boggles my mind because I would have thought that the result of Attempt #1 proves that the active Opera window was found and the keystroke Ctrl-Alt-Right is correctly being read by the ^!Right in the first line. I'm so confused why adding the curly braces in the 2nd line is negating these things that seem as if they should already have occurred):
^!Right::
Send, ^!{Right}
return
Attempt 4: Only differs from #3 with SendPlay instead of Send. The Hotkey again correctly bypasses Windows default display-rotating action. However, the text editor does not appear to do anything at all:
^!Right::
SendPlay, ^!{Right}
return
To summarize:
----------| Curly Braces | No Curly Braces |
----------|------------------|-----------------------------|
Send | Win Dflt Action | types "ight" in text editor |
SendPlay | no action | no action |
So here's the final question, based on this table, it would appear to me that Send is definitely the way to go. If (as Attempt 3 appears to indicate) the curly brace is causing the display-rotation, how do I send this keystroke to the active window without Windows hijacking it? Obviously, #IfWinActive Opera is working correctly when the curly braces aren't used. Perhaps there is another directive that prevents or bypasses Windows' default action entirely?
If you don't wish to have the screen rotation functionality, this can be disabled in your graphics panel. I believe it's associated with Intel graphics application. It might be something like, right-click the desktop, select graphics options, choose hotkeys, then click disable. This is just guessing as I don't have that on the computer I'm using, but I've encountered it on others' computers before.
As for your hotkeys, you need the curly braces around the word "right" or it will just send the individual characters, just like actually typing it. Another issue is that you need a $ in front of your hotkey since it is self-referencing; that is, it's sending the same keystrokes that activate it.
If you haven't had a chance to check out the help file, I definitely recommend it.
https://www.autohotkey.com/docs/Hotkeys.htm#Symbols

How to Release AutoHotkey Control in a Logic Statement?

Goal:
Use the hotkey 'C' to mimmic 'New Email Window' in Outlook, similar to Gmail.
Attempt:
c::
SetTitleMatchMode, 2
If Not WinActive("Message")
Send, ^n
return
Problem:
This script does work, but then inside that 'New Email' Window the 'c' char is locked out, and I can't use it while typing.
Tried Solutions:
I tried adding and empty an 'else {}' but that does not seem to work. Thoughts?
Environment:
Windows 10 / Outlook 2016+
This is probably what you want,
SetTitleMatchMode,2
#if WinActive("Microsoft Outlook")
{
c::
Send, ^n
return
}
This way, it will allow to type the char 'c' anywhere, including the new email window. But, it will trigger if you are in the main Outlook window, opening a new email window for you.
This uses context-sensitive title matching to map C to CTRL+Nwhen "Microsoft Outlook" is in the window title:
SetTitleMatchMode 2 ; All #If statements match anywhere in title
#IfWinActive Microsoft Outlook
c::^n
#IfWinActiv
This worked. I think in my case it needed to know specifically what to do if the window was not active, and in my case it was to fire a regular 'c', I would have thought that logic was built in by default recognize.
c::
SetTitleMatchMode 2
IfWinActive, Outlook
Send, ^n
Else
send, c
return
$c::
If (WinActive("Microsoft Outlook") { ;may/may not be "Microsoft Outlook' use window spy to find out more
send ^N ; if you got that to work then dont mess with this part
} else {
send {c}
}
Return
Basically this checks if Outlook is active then if so sends ^N and if not it will send the character C.
However this isnt the best idea: creating single key hotkeys that are used for typing...
Better idea: you change the hotkey ( the part before "::` ) to something like $^!c
( the $ is so that any other hotkey sending c wouldn't activate this one )
Another aproach would be:
$~c::
If (WinActive(ahk_class "outlook.exe") { ; or something along the lines of that...
sleep 250
send ^N
}
Return
For the most part this does the same thing except it retains the functionality of the c key better, however it may cause issues with typing in outlook so once again,
Please consider either a non-typing single key hotkey such as Rcontrol for example. The ~ in the hotkey means don't revoke original key functionality, The sleep is added to ensure that when you are using the hotkey that the retained c character isn't added to your new email.
Hope this helped ( I am fairly certain that this works ) I don't use outlook so I don't know how well Outlook will respond to this type of thing, and haven't tested this out but I do know my AHK basics and did do function syntax double checking so good luck to you, read my cmments in the code because they are essential to these code snippets functionality.
you may want to add && If (Not WinActive("Message")) ;or whatwver the message window is called if the two snippets didn't work for your needs add this because your new message window may also be called outlook so this will interfere with the typing.

AutoHotkey: How can I paste text into this particular textbox?

Using AutoHotkey, I have a very tiny script to write text:
^m::
SendInput Foo
Return
This works in most places like Notepad and chrome. However, it doesn't work for a particular program I am using. The program has a simple textbox which I can type text into. The program is called TextExpander. I am making sure the cursor is located in the textbox. For some reason, I can't get auto-hotkey to type text into it.
Any help? Tips?
You can look at ControlSetText. Use WindowSpy to check the control name. If it has one, you can use that to set the text.
Alternately, make sure you're trying SendInput/SendEvent/SendPlay.
If you have a way to reliably focus the box you're trying to enter text to, you can put your variable onto Clipboard and send paste via Ctrl+V SendInput, ^v.

(AHK) Creating variable hotkeys that gets the key names from a 2 char file name of a script

I'm trying to make something for our employees to use so that they dont have to alter the script itself to define hotkeys. This may only work for hotkeys which can be defined by a single character, but that's fine, as there are so many combinations that can be made with them, and they can be very easy to remember. The script would look only at 2 character AHK files (or 6 if you must include the extension) in the working directory. And the variables it would search for could be defined with RegEx so for the first hotkey, it would look like ^. and then second would look like .(?=.) Once a match is found, it would simply launch that matched file. Has something like this been done before? It seems so simple but I can't seem to find anything on it.
Edit: Elliot brought this to my attention: http://autohotkey.com/board/topic/60630-easy-editmanage-hotkeyshotstrings-plugin-ahk-l/
It's a neat script manager, and very useful, but it's not what I'm looking for.
I dont not want an additional interface. I want to be able to change the hotkeys by using the filename.
Based on the answer of Forvin. Added the execution of the corresponding ahk script.
#Persistent
SetTimer, FindNewHotkeys, 2000
FindNewHotkeys:
Loop, %A_ScriptDir%\*
{
RegExMatch(A_LoopFileName, "^(.)(.).ahk$", hk)
If (hk)
{
Hotkey, ~%hk1% & ~%hk2%, HotkeyLabel
}
}
Return
HotkeyLabel:
RegExMatch(A_ThisHotkey, "~(.) & ~(.)", hk)
run, %hk1%%hk2%.ahk
Return
#Persistent
SetTimer, FindNewHotkeys, 2000
FindNewHotkeys:
Loop, %A_ScriptDir%\*
{
RegExMatch(A_LoopFileName, "^(.)(.).ahk$", hk)
If (hk)
Hotkey, ~%hk1% & ~%hk2%, HotkeyLabel
}
Return
HotkeyLabel:
MsgBox, A hotkey has been pressed!
Return