Pasting a Tab space

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Redbird
Posts: 13
Joined: 03 Jun 2019, 17:25

Pasting a Tab space

23 Nov 2019, 02:17

Let's say I need to make it so that whenever I press Alt + A, my script pastes a Tab space in the current text editor. I tried this code, but it doesn't paste anything:

Code: Select all

!A::
{
	Clipboard = %A_Tab%
	Send ^v
}
How can I get this to work?
Last edited by Redbird on 23 Nov 2019, 07:12, edited 1 time in total.
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Pasting a Tab space

23 Nov 2019, 05:57

Hallo,
Legacy assignment trims.
Try Expression assignments:

Code: Select all

!a::
Clipboard := A_Tab
Send ^v
Return
or:

Code: Select all

!a::Send,% A_Tab
or send tab key:

Code: Select all

!a::Send, {Tab}
Redbird
Posts: 13
Joined: 03 Jun 2019, 17:25

Re: Pasting a Tab space

23 Nov 2019, 07:17

Thank you. Unfortunately, those codes work in most text editors, but not in the particular program I need the function for: Onenote. The first code does nothing, the other codes create a table, which is default Onenote behavior when pressing Tab.

It seems to be Onenote's fault, though, as attempting to paste a tab space normally from another text editor (ie: without using AHK) does nothing either. However, I can copy tab spaces from Onenote itself and paste it anywhere on Onenote. I don't suppose anyone know how to mimic that behavior with AHK?
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Pasting a Tab space

23 Nov 2019, 13:37

Only a try and only for OneNote:

Code: Select all

!a::Send, {LAlt Down}{Numpad9}{LAlt Up}
Redbird
Posts: 13
Joined: 03 Jun 2019, 17:25

Re: Pasting a Tab space

23 Nov 2019, 14:16

Doesn't do anything.
expertwell
Posts: 1
Joined: 12 Nov 2022, 15:02
Contact:

Re: Pasting a Tab space

12 Nov 2022, 18:44

I know this may be an old question, but this solution may help others who stumble upon it. I experimented with a number of solutions until I found the following one that works.

I discovered that if you paste any character followed by a Tab, OneNote will paste that sequence correctly, Tab included.

For example, if your text is "----abcmno----", and you just paste {Tab} between c and m, nothing happens:
----abcmno----


But if you paste "X"{Tab}, you get:
----abcX	mno----


If course, you don't want an "X" or any visible character where it doesn't belong. So, you can use a space instead, as follows:

Code: Select all

; Example 1.
!A::
{
	tmpClip := Clipboard			; Save current clipboard content
	prefixChr := A_space
	Clipboard := prefixChr . A_Tab
	Send ^v					; paste space followed by Tab
	Sleep 100					; Allow paste to complete before the clipboard changes
	Clipboard := tmpClip			; restore original clipboard content
}
Is that it? Yes, it is. But we can do better.

Perhaps you do not want your tab positioning misaligned by extra space characters. This is why I use a zero-width space character instead, as follows:

Code: Select all

; Example 2.
!A::
{
	tmpClip := Clipboard			; Save current clipboard content
	prefixChr := chr(0x200B) 		; zero-width space
	Clipboard :=   prefixChr . A_Tab
	Send ^v					; paste zero-width space followed by Tab
	Sleep 100					; Allow paste to complete before the clipboard changes
	Clipboard := tmpClip			; restore original clipboard content
}
For the purists, there is one additional possible improvement. Let's say you do not want any extra characters, even if invisible and doesn't affect the display. It would affect the odd search result. So, we actually remove the extra character after pasting:

Code: Select all

; Example 3.
!A::
{
	tmpClip := Clipboard			; Save current clipboard content
	prefixChr:= chr(0x200B) 		; zero-width space
	Clipboard :=   prefixChr . A_Tab
	Send ^v					; paste zero-width space followed by Tab
	Sleep 100					; Allow paste to complete before the clipboard changes
	Send {Left}{BS}{Right}		; skip back over Tab, backspace to remove zero-width char, then skip forward over Tab to original position
	Clipboard := tmpClip			; restore original clipboard content
}
You may have realized by now, that with Example 3, it doesn't matter if prefixChr is "X" or any visible character, because it will be erased anyway.

With the solution of Example 3, we may notice a slight jitter of the cursor as it backtracks to erase the extra character and advances forward again. But the result is exactly what you get were you to use Tab in any regular text editor.

HTH.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Bing [Bot], Peiya, ShatterCoder and 309 guests