Start/End selection with AutoHotkey

autohotkey

I would like to have a script in AutoHotkey that allows me to do the following:

  • When I hit Control + Space:
    It would initiate "text selection mode" (i.e. if I move the cursor with the arrow keys, or moving the mouse it will highlight text)

  • When I hit Control + Space again:
    It would terminate "text selection mode" (e.g. if I move the cursor with the arrow keys or moving the mouse it will not highlight text)

However I would like to avoid using the following strategies, for the reasons explained below:

Strategy 1:

The following script does not let me move the cursor with the keyboard after I initiate text selection. Apparently the computer believes that I am constantly clicking on the mouse location, so it does not let me move the cursor with the keyboard.

*^Space::
text_selection_is_on := !text_selection_is_on
if text_selection_is_on
   Send, {Click down}
else
   Send, {Click up}
return

Strategy 2:

The following script relies on simulating the action of pressing the shift key down to initiate text selection. However, I would like to avoid relying on the shift key since some of the programs that I plan on using this script with require the shift key to be up (i.e. not pressed) when I move the cursor to select text.

*^Space::
text_selection_is_on := !text_selection_is_on
if text_selection_is_on
   Send, {Shift down}
else
   Send, {Shift up}
return

Is it possible to do this with AutoHotkey? If so, how?

Thanks!

Best Answer

Very ugly but maybe it's the solution you were looking for. Very sad the shift down solution doesn't work for you, because that's what I've always been using.

*^Space::
dx = 1
dy = 1
text_selection_is_on := !text_selection_is_on
if text_selection_is_on
{
   MouseMove, %A_CaretX%, %A_CaretY%, 0
   dx := A_CaretX
   Send, {right}
   dx := A_CaretX - dx
   Send, {left}
   dy := A_CaretY
   Send, {down}
   dy := A_CaretY - dy
   Send, {up}
   Send, {Click down}
}
else
   Send, {Click up}
return

left::
if text_selection_is_on
    MouseMove, % -dx, 0, 0, R
else
   Send, {left}
return

right::
if text_selection_is_on
    MouseMove, % dx, 0, 0, R
else
   Send, {right}
return

down::
if text_selection_is_on
    MouseMove, 0, % dy, 0, R
else
   Send, {down}
return

up::
if text_selection_is_on
    MouseMove, 0, % -dy, 0, R
else
   Send, {up}
return
Related Question