I am a total newbie to AHK with a very simple question. If I am editing in MS Word or Powerpoint, how can I use AHK to send a series of MS keyboard shortcuts as if I typed them. For example,
ALT (opens menu selection for all items on the ribbon)
H (opens Home Menu)
1 (selects BOLD)
Can someone point me to the syntax that does this or send me a code snippet or reference to do this?
Thanks
sending a sequence of keystrokes in MS Word Topic is solved
Re: sending a sequence of keystrokes in MS Word Topic is solved
Send command. For your example, it would be Send {Alt}h1 -- notice the h is uncapitalized. In the Send command, capital H counts as Shift+h which may result in a different action.
However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: sending a sequence of keystrokes in MS Word
On the other hand, many times, this time included MS Word can handle the keyboard commands coming very quickly.Exaskryz wrote:However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
This line at the top of your script will make the Send command send the text pretty much as fast as possible.
Code: Select all
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: sending a sequence of keystrokes in MS Word
You have to take into consideration the speed of the computer as well thus a break between them will ensure it will work. My computer can't handle it without sleep.
It switches slower and thus sends everything to fast.
It switches slower and thus sends everything to fast.
FanaticGuru wrote:On the other hand, many times, this time included MS Word can handle the keyboard commands coming very quickly.Exaskryz wrote:However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
This line at the top of your script will make the Send command send the text pretty much as fast as possible.FGCode: Select all
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: sending a sequence of keystrokes in MS Word
That is not my experience at all with MS Word.Johana wrote:You have to take into consideration the speed of the computer as well thus a break between them will ensure it will work. My computer can't handle it without sleep.
It switches slower and thus sends everything to fast.
Code: Select all
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
F12::
Loop, 50
Send {Alt}h1
MsgBox DONE
return
Lots of programs that don't handle buffering of keystrokes, sure, lots of problems sending things too fast. Programs like MS Word that buffers properly, no problem.
At least that is my experience with MS Word on various computers with different versions of MS Word.
On the other side of the coin, everyone needs to do what works for them and from my experiment it does not matter how fast I send the keys it still takes MS Word about 200 ms to process the 'bold' toggle, so some small Sleeps are probably not really going to slow the process down. Sending really fast and buffering still has the advantage that normal user keystrokes go at the end of the buffer and do not get slipped in the middle of a string being sent by AHK.
SetKeyDelay can be useful if the program requires the key strokes to come in really slow like many games do. Unlike MS Word, they purposefully will not accept unhumanly fast keystrokes.
But really since I discovered the wonders of COM, I don't really send keystrokes to MS Word anymore.
Code: Select all
F12::
ComObjActive("Word.Application").Selection.Font.Bold := 9999998
return
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: sending a sequence of keystrokes in MS Word
I am so new that never Heard about the COM. Sorry for hijacking the thread.
Regarding the Word: I am not sure it does buffer? We have "templates" in Word that we open where we have to change some things. To "jump" between where we need to change we press F11. And sometimes we need to press F11 5 times, then enter something, then press F11, then erase. Everything I've done with a GUI so it basically pastes variables.
If I don't have 300 sleep, it will input Everything so fast that Word doesn't have a chance. Everything gets on the first line..
Regarding the Word: I am not sure it does buffer? We have "templates" in Word that we open where we have to change some things. To "jump" between where we need to change we press F11. And sometimes we need to press F11 5 times, then enter something, then press F11, then erase. Everything I've done with a GUI so it basically pastes variables.
If I don't have 300 sleep, it will input Everything so fast that Word doesn't have a chance. Everything gets on the first line..
Johana wrote:You have to take into consideration the speed of the computer as well thus a break between them will ensure it will work. My computer can't handle it without sleep.
It switches slower and thus sends everything to fast.
FanaticGuru wrote:On the other hand, many times, this time included MS Word can handle the keyboard commands coming very quickly.Exaskryz wrote:However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
This line at the top of your script will make the Send command send the text pretty much as fast as possible.FGCode: Select all
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
- FanaticGuru
- Posts: 1906
- Joined: 30 Sep 2013, 22:25
Re: sending a sequence of keystrokes in MS Word
For me on run of the mill office computers with MS Word 2010 & MS Word 365 running Windows 10, this works flawlessly:Johana wrote:I am so new that never Heard about the COM. Sorry for hijacking the thread.
Regarding the Word: I am not sure it does buffer? We have "templates" in Word that we open where we have to change some things. To "jump" between where we need to change we press F11. And sometimes we need to press F11 5 times, then enter something, then press F11, then erase. Everything I've done with a GUI so it basically pastes variables.
If I don't have 300 sleep, it will input Everything so fast that Word doesn't have a chance. Everything gets on the first line..
Code: Select all
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
F12::
Loop, 50
Send {F11} Field Number: %A_Index%
return
Could be a particularity of your script or a different software setup.
If you ever have any questions on how to do things with Word, Excel or Outlook through COM, you can generally get quite a bit of help from me or others on the forum. It is very powerful.
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
-
- Posts: 9
- Joined: 14 Jan 2023, 13:15
Re: sending a sequence of keystrokes in MS Word
Sorry to resurrect this old thread... but I'm having a similar problem in 2023 (with AHK 2.0). In short, I can't get word to hear an Alt from AHK.
In more detail:
I'm trying to replicate the google shortcut to create a bulleted list in word (It's shift control 7)
I've got the following hotkey assignment:
If I press shift control 7, Word receives the h, u, up and enter fine, but never gets the alt.
If I press alt manually, then shift control 7, it works perfectly.
Any ideas how to get "alt" through to word?
In more detail:
I'm trying to replicate the google shortcut to create a bulleted list in word (It's shift control 7)
I've got the following hotkey assignment:
Code: Select all
+^7::
{
Send "{alt}"
sleep 100
Send "h"
Sleep 100
send "u"
Sleep 100
send "{up}"
sleep 100
send "{enter}"
}
If I press alt manually, then shift control 7, it works perfectly.
Any ideas how to get "alt" through to word?
-
- Posts: 9
- Joined: 14 Jan 2023, 13:15
Re: MS Word and AutoHotkey
replying to my own question a few minutes ago....
Send "!h"
...is the key - !h gets through where {alt}h doesn't.
(And it turns out that to toggle bullets, you need to do " send "!h{down}{right 10}{up}{enter}"" - but that's a quirk of the Word interface rather than anything related to AHK!)
Hope that helps anyone who finds themselves in the same situation....
Send "!h"
...is the key - !h gets through where {alt}h doesn't.
(And it turns out that to toggle bullets, you need to do " send "!h{down}{right 10}{up}{enter}"" - but that's a quirk of the Word interface rather than anything related to AHK!)
Hope that helps anyone who finds themselves in the same situation....
Re: MS Word and AutoHotkey
Why in a different topic, though? I guess, it was just a mistake because your other post hadn't been approved yet.
So no worries, I have moved your second post to this topic here (where I found the first post).
PS:
Yes, ! will work as a modifier for the following key, while just sending {Alt} will already release the Alt key before sending the next key -> no modifier.
Alternatively, one could also try Send "{Alt down}h{Alt up}" (v2 syntax).
-
- Posts: 9
- Joined: 14 Jan 2023, 13:15
Re: MS Word and AutoHotkey
That's very helpful, thanks gregster
Brilliant, that's just what I needed to know - thank you
Who is online
Users browsing this forum: doodles333, Google [Bot], mikeyww and 290 guests