Some problems in a long but seemingly simple script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hotter
Posts: 9
Joined: 27 Nov 2022, 17:20

Some problems in a long but seemingly simple script

25 Feb 2023, 16:02

I want to use the hotkey to highlight the word on which the cursor is.
If the cursor is at the beginning of a word (and there is a space to the LEFT of the cursor) - then highlight the word on the RIGHT
If the cursor is at the end of a word (and there is a space to the RIGHT of the cursor) - then highlight the word on the LEFT
In theory, a script with such logic should work:

Code: Select all

#a::
SelectWordUnderCaret(){
	leftChar := GetLeftChar()
	MsgBox, %leftChar% ;nothing here!
	if !InStr(leftChar, " ") ;logic: if cursor not at beginning of selected word
	{
		JumpToWordBeginning()
	}
	SelectWordToRight()
}
JumpToWordBeginning(){
	Send, ^{Left}
	return
}
SelectWordToRight(){
	Send, ^+{Right}
	return
}

GetLeftChar(){
	CancelOldSelection()
	SelectLeftChar()
	leftChar := CopySelectedWordNoClip()
	return leftChar
}
CancelOldSelection(){
	SendInput, {Right}
	SendInput, {Left}
	return
}
SelectLeftChar(){
	SendInput, +{Left}
 return
}

CopySelectedWordNoClip(){ ;use that so my Clipboard keep last clip
	oldClip := ClipboardAll
	Clipboard := 
	SendInput, ^c
	ClipWait
	result := ClipboardAll
	Clipboard := oldClip
	return result 
}
it works, but serious bugs:
1) there is a void in the String variable. If I log it in

Code: Select all

MsgBox, %leftChar%
, then nothing is visible
2) if the cursor is between the 1st and 2nd letter, then selects the left word. For some reason, the logic has shifted 1 character to the right.
3) after executing the logic, sometimes it remains either Ctrl or Shift. (In my Sublime IDE, when the mouse is clicked, lines are multi-separated, new cursors are created). As if some of the functions did not have time to "programmatically release" them. Should I instead of

Code: Select all

Send, ^{Left}
use something like:

Code: Select all

Send {Ctrl down}
Send {Left}
Send {Ctrl up}
Can You help me with this 3 problem?


[Mod action: Moved topic to v1 section. The main section is for v2.]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 120 guests