Page 1 of 1

Putting characters before and after text

Posted: 14 Apr 2019, 13:18
by nijwons
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.

Re: Putting characters before and after text  Topic is solved

Posted: 14 Apr 2019, 15:23
by JoeWinograd
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

Re: Putting characters before and after text

Posted: 14 Apr 2019, 16:12
by Osprey
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.

Re: Putting characters before and after text

Posted: 14 Apr 2019, 16:59
by JoeWinograd
> creates hotkeys for each key programatically

Great idea, Osprey! Regards, Joe

Re: Putting characters before and after text

Posted: 15 Apr 2019, 10:22
by nijwons
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!

Re: Putting characters before and after text

Posted: 15 Apr 2019, 11:02
by JoeWinograd
> 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

Re: Putting characters before and after text

Posted: 15 Apr 2019, 12:12
by nijwons
Yeah, I've tried it but when running the script I cannot type characters, nothing happens.

Thanks.

Re: Putting characters before and after text

Posted: 15 Apr 2019, 12:16
by JoeWinograd
Works perfectly here...I tested it well before posting. Please post the full script that you're using.

Re: Putting characters before and after text

Posted: 15 Apr 2019, 12:17
by nijwons
Wait sorry, my bad, figured it out!

Re: Putting characters before and after text

Posted: 15 Apr 2019, 12:17
by nijwons
Wait sorry, my bad, figured it out!

Re: Putting characters before and after text

Posted: 15 Apr 2019, 12:23
by JoeWinograd
Great news!

Re: Putting characters before and after text

Posted: 15 Apr 2019, 12:30
by nijwons
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!

Re: Putting characters before and after text

Posted: 15 Apr 2019, 12:56
by JoeWinograd
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

Re: Putting characters before and after text

Posted: 15 Apr 2019, 13:04
by nijwons
Yes, thank you.

Re: Putting characters before and after text

Posted: 15 Apr 2019, 13:04
by JoeWinograd
Our messages just crossed. See the update to my previous post.

Re: Putting characters before and after text

Posted: 15 Apr 2019, 13:09
by nijwons
Thank you so much!

Re: Putting characters before and after text

Posted: 15 Apr 2019, 13:14
by JoeWinograd
You're very welcome!

Re: Putting characters before and after text

Posted: 23 May 2019, 14:54
by nijwons
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.