Replace 2 space characters with one

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JPh1
Posts: 42
Joined: 18 Jan 2018, 22:06

Replace 2 space characters with one

22 Oct 2018, 15:23

Every now and then, my malfunctioning keyboard spacebar types 2 spaces instead of one. I'd like to automatically correct it with a AHK hotstring.
I looked at A_Space, AutoTrim, etc. without finding a clear way to do it.

How would you replace 2 spaces with one?
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Replace 2 space characters with one

22 Oct 2018, 15:45

Hi,

You should try it with SetTimer.

Code: Select all

Space::
if space_presses > 0 ; SetTimer already started, so we log the keypress instead.
{
      space_presses += 1
      return
}
; Otherwise, this is the first press of a new series. Set count to 1 and start
; the timer:
space_presses = 1
SetTimer, Space, -400 ; Wait for more presses within a 400 millisecond window.
return
I cannot test the script now.

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
JPh1
Posts: 42
Joined: 18 Jan 2018, 22:06

Re: Replace 2 space characters with one

22 Oct 2018, 16:00

Thanks, but this is rather involved. I was hoping to do it with a single hotstring...
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Replace 2 space characters with one

22 Oct 2018, 16:27

Perhaps this, but there may be some unanticipated drawback (or it might be fine). Cheers.

Code: Select all

:*?b0:  ::{Backspace}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
JPh1
Posts: 42
Joined: 18 Jan 2018, 22:06

Re: Replace 2 space characters with one

22 Oct 2018, 16:48

It seems to be working. What does *?b0 mean and what sort of drawback were you thinking of?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Replace 2 space characters with one

22 Oct 2018, 17:21

* ? and b0 are described here:
Hotstrings - Definition & Usage | AutoHotkey
https://autohotkey.com/docs/Hotstrings.htm

My summary:
* and ?: Hotstrings can be triggered without an ending character (*) and inside words (?).
b0: Normally with hotstrings, you type the abbreviation manually, and AutoHotkey then sends backspaces to remove the abbreviation, and AutoHotkey then sends text or does some action(s). b0 prevents sending backspaces.

Re. drawbacks, perhaps you would type too quickly, and get a backspace that deleted the wrong character. Generally speaking you have to test things for a while e.g. a day or so, to see if anything unexpected comes up.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Replace 2 space characters with one

22 Oct 2018, 19:00

I had the same idea with backspace, but in my opinion it is a risky solution.
What if he uses space in a game or for other applications?

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750, septrinus and 234 guests