how to trigger expansion of a Hotstring without moving cursor Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
GavinPen
Posts: 22
Joined: 24 Oct 2017, 16:49
GitHub: GavinPen

how to trigger expansion of a Hotstring without moving cursor

10 Jun 2018, 15:52

I want to be able to press some key to trigger the expansion of a Hotstring (any of my Hotstrings), but avoid moving the cursor
(ie leave the cursor in the same place, just after the end of the expansion, without actually outputting any visible character or whitespace).
None of the default EndChars do this
User avatar
JoeWinograd
Posts: 1249
Joined: 10 Feb 2014, 20:00

Re: how to trigger expansion of a Hotstring without moving cursor

10 Jun 2018, 16:04

Hi Gavin,
If you put an asterisk between the first two colons, the hotstring will expand immediately, i.e., no EndChar is needed, and the cursor will stay in the same place...at the end of the expansion...no chars or whitespace after it. Regards, Joe
GavinPen
Posts: 22
Joined: 24 Oct 2017, 16:49
GitHub: GavinPen

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 04:19

Thanks Joe.
But I actually do want my EndChars to be output / move the cursor on most of the time;
it's just that occasionally, I need to expand my abbreviations without moving the cursor.
I hoped I could do this via a special EndChar (maybe one that doesn't normally type anything, like 'Esc').
Apologies - this isn't clear in my original post!
Gavin
User avatar
JoeWinograd
Posts: 1249
Joined: 10 Feb 2014, 20:00

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 12:30

Hi Gavin,
If I'm understanding you right, you want some EndChars to expand the hotstrings and move the cursor and send the EndChar; but you want other EndChars (or maybe just one EndChar) to expand the hotstrings and not move the cursor and not send the EndChar. If so, I don't know how to do that, as I'm not aware of how to take different actions on the same hotstring based on which EndChar was pressed to trigger it. But I'm not an expert in hotstrings, so let's hope that someone else jumps in. Regards, Joe
gregster
Posts: 2818
Joined: 30 Sep 2013, 06:48

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 12:43

Perhaps Input (especially example 2) as a workaround. But you would probably have to define the "hotstrings" in question a second time in the hotkey definition.
Also, set a variable (temporarily, to false) in the hotkey definition that can make your "normal" hotstrings inactive via context-sensitivity, so that they won't interfear with the actions for the Input strings.
GavinPen
Posts: 22
Joined: 24 Oct 2017, 16:49
GitHub: GavinPen

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 16:49

thanks both.
Joe - yes, that's what I wanted.
Gregster - I think your suggestion could possibly work, but it looks complicated!
There isn't an easily defined context where I want to to use the 'non-moving' EndChar - I'd probably use it most often to correct things that I've mistyped, but this could be in any window.
gregster
Posts: 2818
Joined: 30 Sep 2013, 06:48

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 16:57

The context would be just the state of a variable in the hotkey code, set by a keypress ("I want to be able to press some key to trigger the expansion of a Hotstring"). I will try it and show what I mean, if it works...

Of course that key needs to be pressed before you enter the specific string.
Last edited by gregster on 11 Jun 2018, 17:02, edited 1 time in total.
User avatar
JoeWinograd
Posts: 1249
Joined: 10 Feb 2014, 20:00

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 17:00

Gavin,
Would this effort be worth it? The alternative is simply having to press the backspace key in those cases where you don't want the cursor to move and the EndChar to appear. You would save one keystroke in those cases — the special EndChar instead of a normal EndChar and backspace. I suppose if you do it enough that it's worth it, but your comments indicate that it's just an occasional occurrence. Regards, Joe
Last edited by JoeWinograd on 11 Jun 2018, 17:08, edited 1 time in total.
gregster
Posts: 2818
Joined: 30 Sep 2013, 06:48

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 17:05

Re-reading your posts, I am actually not totally sure, what you are exactly trying to do, Gavin. Can you perhaps give a more detailed example?
I am not absolutely sure that what you are looking for cannot be already done with the existing Hotstring options.
GavinPen
Posts: 22
Joined: 24 Oct 2017, 16:49
GitHub: GavinPen

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 17:31

You're right, Joe - I wouldn't save any keystrokes by using the context variable, so it wouldn't be worth the effort (though I appreciate your input, Gregster)
It is a shame that it seems to be impossible just to add a non-typing character to the EndChars list.
Here's an example of what I'm looking for:
say I've typed a sentence including a word that I want to replace, eg
'Madrid is the capital of France'
if I start with my cursor at the beginning of the line,
then select 'Madrid' using Ctrl+Shift+Right,
then overtype it using an abbreviation for Paris (say 'prs'),
then if I press the spacebar, the abbreviation is expanded to 'Paris'.
However, the space appears after 'Paris', in addition to the space which was already after 'Madrid' - giving 2 spaces.
If I could instead use an EndChar that didn't type anything, I'd only see the single original space.
Of course, I can work round this by pressing Backspace - but it's a wee bit annoying!
I use hotstrings to type almost everything, so fixing mistakes like this one is something I do several times a day.
gregster
Posts: 2818
Joined: 30 Sep 2013, 06:48

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 17:32

GavinPen wrote:I want to be able to press some key to trigger the expansion of a Hotstring (any of my Hotstrings), but avoid moving the cursor
(ie leave the cursor in the same place, just after the end of the expansion, without actually outputting any visible character or whitespace).
None of the default EndChars do this
If you just want to have different actions depending on different end characters, you can do seomthing like this:

Code: Select all

::te::
if (A_endchar = A_space)
	Sendinput test%A_space%
else if (A_endchar = "`t")
	return
else
	Sendinput test{left 4}
return
Type te and space and get "test " (including trailing space), type te and Tab and erase everything you typed while resetting the caret to the place where you started and type te and Enter (or one of the other end characters and just get the string "test" (caret at the beginning of the word).

But there are also other variants possible...
gregster
Posts: 2818
Joined: 30 Sep 2013, 06:48

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 17:41

GavinPen wrote:'Madrid is the capital of France'
if I start with my cursor at the beginning of the line,
then select 'Madrid' using Ctrl+Shift+Right,
then overtype it using an abbreviation for Paris (say 'prs'),
then if I press the spacebar, the abbreviation is expanded to 'Paris'.
However, the space appears after 'Paris', in addition to the space which was already after 'Madrid' - giving 2 spaces.
If I could instead use an EndChar that didn't type anything, I'd only see the single original space.
Of course, I can work round this by pressing Backspace - but it's a wee bit annoying!
Tab just replaces, Space adds also a space.

Code: Select all

::prs::
if (A_endchar = "`t")
	Sendinput Paris
If (A_endchar = A_space)
	Sendinput Paris%A_space%
return
User avatar
JoeWinograd
Posts: 1249
Joined: 10 Feb 2014, 20:00

Re: how to trigger expansion of a Hotstring without moving cursor

11 Jun 2018, 17:45

Select "Madrid " instead of "Madrid". Or simply double-click Madrid, which, in all products that I'm aware of that deal with text, will also select the ending space. That said, I hear you on wanting to be able to use a non-typing character as an EndChar.
gregster
Posts: 2818
Joined: 30 Sep 2013, 06:48

Re: how to trigger expansion of a Hotstring without moving cursor  Topic is solved

11 Jun 2018, 18:03

A way to define the same behaviour for multiple hotstrings:

Code: Select all

::brl::
::mdr::
::prs::
::wsh::
abbr := {"prs":"Paris", "mdr" :"Madrid", "brl":"Berlin", "wsh":"Washington, D.C."}
key := Substr(A_ThisHotkey, 3)

If (A_endchar = "`t")
	Sendinput % abbr[key]
else If (A_endchar = A_space)
	Sendinput % abbr[key] A_space
else
	Sendinput % abbr[key] A_endchar	
return
Still Tab just to replace, Space to add an space. Of course, additional end characters can be defined.

Edit: Now all other end characters (Enter, /, . etc. will appear as themselves... Edit2: actually {, } and ! would need a workaround and their own handling, if you need them. (SendInput is the culprit)
GavinPen
Posts: 22
Joined: 24 Oct 2017, 16:49
GitHub: GavinPen

Re: how to trigger expansion of a Hotstring without moving cursor

12 Jun 2018, 16:22

Thanks Gregster, your solution works! I've added handling for the other 3 characters:

Code: Select all

::brl::
::mdr::
::prs::
::wsh::
abbr := {"prs":"Paris", "mdr" :"Madrid", "brl":"Berlin", "wsh":"Washington, D.C."}
key := Substr(A_ThisHotkey, 3)

Sendinput % abbr[key]
If (A_endchar = A_space)
	Sendinput %A_space%
else If (A_endchar = "!")
	Sendinput {!}
else If (A_endchar = "{")
	Sendinput {{}
else If (A_endchar = "}")
	Sendinput {}}	
else If (A_endchar = "`t") {
	}
else
	Sendinput %A_endchar%
return
Last edited by GavinPen on 12 Jun 2018, 16:31, edited 1 time in total.
GavinPen
Posts: 22
Joined: 24 Oct 2017, 16:49
GitHub: GavinPen

Re: how to trigger expansion of a Hotstring without moving cursor

12 Jun 2018, 16:27

JoeWinograd wrote:simply double-click Madrid, which, in all products that I'm aware of that deal with text, will also select the ending space
Double-clicking does select the ending space in Microsoft Word, but not in Notepad++ or Sublime, which I use much more often
User avatar
JoeWinograd
Posts: 1249
Joined: 10 Feb 2014, 20:00

Re: how to trigger expansion of a Hotstring without moving cursor

12 Jun 2018, 17:07

Fair enough...I don't use NPP or Sublime. My fav text editor (KEDIT) selects the ending space, as does Chrome, Firefox, IE, Opera, Notepad, Vivaldi, Word, WordPad, among others. Anyway, I'm glad you found the solution...clever stuff from gregster! Regards, Joe

Return to “Ask For Help”

Who is online

Users browsing this forum: Billykid, boiler, flyingDman and 151 guests