Putting characters before and after text Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
nijwons
Posts: 9
Joined: 14 Apr 2019, 12:48

Putting characters before and after text

14 Apr 2019, 13:18

So i was wondering if there was a way to automatically put characters before text and before you press enter, so say you typed "Hello", it would come out /Hello/after you pressed enter.

Thanks, any help would be appreciated.
User avatar
JoeWinograd
Posts: 1231
Joined: 10 Feb 2014, 20:00

Re: Putting characters before and after text  Topic is solved

14 Apr 2019, 15:23

Hi nijwons,

I see that you joined our group today and that this is your first post, so let me start by saying...Welcome Aboard!

I'm not an expert in hotkeys and hotstrings, so there may be a much better way to do this, but my first thought is to define every key on the keyboard that produces a character as a hotstring that appends itself to a current-text string and then sends itself. At the end, the Enter key (via a hotkey, not hotstring) sends the current-text string, prefixed and suffixed by the specified characters, and resets the current-text string to null.

Here's the beginning of such a script to get you going:

Code: Select all

#Warn,UseUnsetLocal ; warn when local variable is used before it is set
#NoEnv ; do not check empty variables to see if they are environment variables
#SingleInstance Force ; skip dialog box and replace old instance immediately
SetBatchLines,-1 ; run at maximum speed
SendMode Input ;  generally faster and more reliable

BeforeTextChar:="<" ; change this to whatever you want
AfterTextChar:=">" ; change this to whatever you want
CurrentText:=""

:c*:a::
CurrentText:=CurrentText . "a"
Send a
Return

:c*:A::
CurrentText:=CurrentText . "A"
Send A
Return

:*:0::
CurrentText:=CurrentText . "0"
Send 0
Return

:*: ::
CurrentText:=CurrentText . A_Space
Send %A_Space%
Return

:*:<::
CurrentText:=CurrentText . "<"
Send <
Return

:*:>::
CurrentText:=CurrentText . ">"
Send >
Return

:*:/::
CurrentText:=CurrentText . "/"
Send /
Return

:*:\::
CurrentText:=CurrentText . "\"
Send \
Return

~Enter::
CurrentText:=BeforeTextChar . CurrentText . AfterTextChar
Send %CurrentText%{Enter}
CurrentText:=""
Return
To show the method, I've included hotstrings for the upper and lower case letter A, number 0, space, a few special chars (<, >, /, and \), and, of course, the hotkey for the Enter key.

You'll have to expand the script to include all upper and lower case letters, all numbers, and all special chars. Note that c in the hotstring means that it is case sensitive and * in the hotstring means that an ending character is not required to trigger it. Also, note that ~ in the Enter hotkey means that its native function will not be blocked when it fires. Regards, Joe
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Putting characters before and after text

14 Apr 2019, 16:12

My idea is similar to Joe's, but does what the TooltipKeypress script does and creates hotkeys for each key programatically:

Code: Select all

Loop, 5000
{
    Hotkey, % "~*" GetKeyName(Format("vk{:x}", a_index - 1)), GetText	; Create lowercase hotkeys
    Hotkey, % "~*+" GetKeyName(Format("vk{:x}", a_index - 1)), GetText	; Create uppercase hotkeys
}

GetText:
    Sleep, 50
    Key := SubStr(A_ThisHotkey, 3)    				; Remove the first 2 characters ("~*") from the returned string
    If(SubStr(Key, 1, 1) = "+")
        Text .= Format("{:U}", SubStr(Key, 2, 1))	; Convert the 2nd character (after the "+") to uppercase
    Else If(StrLen(Key) = 1)
        Text .= Key
    Else If(Key = "Enter")
    {
        NumBack := StrLen(Text) + 1					; Backspace the length of the string + 1 for Enter
        Send, {Backspace %NumBack%}/%Text%/
        NumBack := 0
        Text := ""
    }
Return
That appears to do what it sounds like you're asking for.
Last edited by Osprey on 14 Apr 2019, 17:05, edited 3 times in total.
User avatar
JoeWinograd
Posts: 1231
Joined: 10 Feb 2014, 20:00

Re: Putting characters before and after text

14 Apr 2019, 16:59

> creates hotkeys for each key programatically

Great idea, Osprey! Regards, Joe
nijwons
Posts: 9
Joined: 14 Apr 2019, 12:48

Re: Putting characters before and after text

15 Apr 2019, 10:22

Thank you so much, is there way to return the string as you press enter, so that when you sent a message it would send it as /Hello/? Sorry if this is vaguely worded, im not sure how to word it well. Again, thank you so much for the answer!
User avatar
JoeWinograd
Posts: 1231
Joined: 10 Feb 2014, 20:00

Re: Putting characters before and after text

15 Apr 2019, 11:02

> is there way to return the string as you press enter, so that when you sent a message it would send it as /Hello/

That's what my script does. When you press Enter, it executes this command:

Code: Select all

Send %CurrentText%{Enter}
The CurrentText variable has the text that you entered, prefixed by the BeforeTextChar character and suffixed by the AfterTextChar character (in your case, you want both to be a forward slash - / - but I made them variables so that the solution is more general). Regards, Joe
nijwons
Posts: 9
Joined: 14 Apr 2019, 12:48

Re: Putting characters before and after text

15 Apr 2019, 12:12

Yeah, I've tried it but when running the script I cannot type characters, nothing happens.

Thanks.
User avatar
JoeWinograd
Posts: 1231
Joined: 10 Feb 2014, 20:00

Re: Putting characters before and after text

15 Apr 2019, 12:16

Works perfectly here...I tested it well before posting. Please post the full script that you're using.
nijwons
Posts: 9
Joined: 14 Apr 2019, 12:48

Re: Putting characters before and after text

15 Apr 2019, 12:17

Wait sorry, my bad, figured it out!
nijwons
Posts: 9
Joined: 14 Apr 2019, 12:48

Re: Putting characters before and after text

15 Apr 2019, 12:17

Wait sorry, my bad, figured it out!
nijwons
Posts: 9
Joined: 14 Apr 2019, 12:48

Re: Putting characters before and after text

15 Apr 2019, 12:30

Is it possible to the before text above the CurrentText and the AfterText below the CurrentText, sorry, I'm new to AHK, trying to figure it out and you've been a huge help, thank you!
User avatar
JoeWinograd
Posts: 1231
Joined: 10 Feb 2014, 20:00

Re: Putting characters before and after text

15 Apr 2019, 12:56

Not sure I understand...do you mean that you want this:

/
Hello
/

If so, then simply put a new line character - `n - before and after CurrentText. In other words, change the assignment statement in the Enter hotkey to this:

Code: Select all

CurrentText:=BeforeTextChar . "`n" . CurrentText . "`n" . AfterTextChar
If that's not what you want, please explain in more detail. Regards, Joe
Last edited by JoeWinograd on 15 Apr 2019, 13:04, edited 1 time in total.
User avatar
JoeWinograd
Posts: 1231
Joined: 10 Feb 2014, 20:00

Re: Putting characters before and after text

15 Apr 2019, 13:04

Our messages just crossed. See the update to my previous post.
nijwons
Posts: 9
Joined: 14 Apr 2019, 12:48

Re: Putting characters before and after text

23 May 2019, 14:54

Hey, I know this is a very late followup, but is there a way to send them all in one message instead of 3? Currently it sends / then hello then / instead of them all in the same message on different lines

Thanks.

Return to “Ask For Help”

Who is online

Users browsing this forum: flyingDman, Google [Bot], WilburBr and 156 guests