Pasting parts of code with ahk?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kiwY12
Posts: 1
Joined: 17 May 2022, 12:00

Pasting parts of code with ahk?

Post by kiwY12 » 17 May 2022, 12:04

I would like to create a tool, which when triggered, pastes some specific parts of code.
For example, when I press:
^!#d:: //the dijkstra algorithm gets pasted
^!#k:: //Kruskal's MST algorithm gets pasted
and so on.

When I try a simple "Send <code> ", ahk doesn't like it, because it gets confused by some keywords used in the pre-written algorithm, and doesn't work with it as a simple string.

User avatar
mikeyww
Posts: 26595
Joined: 09 Sep 2014, 18:38

Re: Pasting parts of code with ahk?

Post by mikeyww » 17 May 2022, 12:40

Welcome to this AutoHotkey forum!

I recommend:
1. Post your complete script
2. What happens when you run the script?
3. Exactly what should happen instead (be specific)?

To send text without interpreting special sequences, use text mode.

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Pasting parts of code with ahk?

Post by jarhead » 17 May 2022, 12:41

Try SendInput or SendRaw or Send {Text}?

^!#d::
SendInput {Text}, dijkstra algorithm

^!#d::
SendRaw, dijkstra algorithm

^!#d::
Send {Text}, dijkstra algorithm

Ianizer
Posts: 79
Joined: 07 Mar 2021, 00:06

Re: Pasting parts of code with ahk?

Post by Ianizer » 17 May 2022, 13:03

Try this (replace the <code> with the actual text/code that you want to be pasted):

Code: Select all

^!#d::
	OldClipboard := ClipboardAll ;store the current clipboard data because it will soon be changed
	Clipboard := "
(
<code>
)" ;put the text/code in the clipboard
	Send, ^v ;paste whatever's currently in the clipboard
	Clipboard := OldClipboard ;restore previous clipboard data
	OldClipboard := "" ;no need to store the previous clipboard data anymore
return

^!#k::
	OldClipboard := ClipboardAll ;store the current clipboard data because it will soon be changed
	Clipboard := "
(
<code>
)"
	Send, ^v ;paste whatever's currently in the clipboard
	Clipboard := OldClipboard ;restore previous clipboard data
	OldClipboard := "" ;no need to store the previous clipboard data anymore
return

Post Reply

Return to “Ask for Help (v1)”