Moving Cursor to defined area with keyboard shortcut

Hi,

Very new with this app but I wanted to know a script for moving the cursor to a new field that I define such as [ ] (or any other symbol) with a keyboard shortcut. Some background: I use dragon medical one dictation and in it I can create keyboard shortcuts for very quickly moving the mouse cursor to different parts on the screen and continue dictating. I’m trying to replicate this in Drafts. I figured out how to edit the “Move Cursor” with keyboard shortcuts in the Manage Actions section, but the only advice I saw was moving it one word, to the end of the line, or end fo the draft etc. I’d like to be able to use the keyboard to move it to this symbol [ ] and can keep toggling through forward and backwards through the symbols.

Would love some help! Thanks.

Here are a couple of functions that should help.

This first one is a function to jump to the next opening marker of one of your entry fields. The line that calls the function is at the end of the block and you can pass in whatever unique opening field marker you wish to use. In this case an open square bracket. The function gets the current cursor position, searches forwards for the next occurrence of the open field marker from that point forwards and then sets the position of the cursor to be just after the opening field marker. If the function moves the cursor it returns true. If there are no more occurrences after the current cursor position, the cursor stays exactly where it is and the function returns false.

function cursorMoveAfterNext(p_strMarkerStart)
{
	let intCurrentPos = editor.getSelectedRange()[0];
	let intNextMarkerStartPos = editor.getText().indexOf(p_strMarkerStart, intCurrentPos)
	if(intNextMarkerStartPos != -1)
	{
		editor.setSelectedRange(intNextMarkerStartPos + p_strMarkerStart.length, 0);
		editor.activate();
		return true;
	}
	else
	{
		editor.activate();
		return false;
	}
}

cursorMoveAfterNext("[");

The inverse function operates slightly differently. It uses both the opening and closing field markers. This is because we could be in a field or after a field and need to use the closing marker to work out the previous field, but then the opening marker to place the position of the cursor. Should you want to just stay at the end of the field, you should be able to figure out what to amend in this function and simplify it down to something more akin to that first function.

The process overall is very similar. Search backwards from the current cursor position, for the last instance of the closing field marker. Once found, then search backwards for the last occurrence of the opening field marker. The cursor is then placed after that opening field marker (based on position and marker length). Once again, the cursor remains where it is if no previous field is found, and returns trues for if the cursor was moved, and false for if it was not.

function cursorMoveAfterPrevious(p_strMarkerStart, p_strMarkerEnd)
{
	let intCurrentPos = editor.getSelectedRange()[0];
	let strPrevious = editor.getText().slice(0, intCurrentPos);
	let intPrevMarkerEndPos = strPrevious.lastIndexOf(p_strMarkerEnd, intCurrentPos);
	strPrevious = strPrevious.slice(0, intPrevMarkerEndPos);
	let intPrevMarkerStartPos = strPrevious.lastIndexOf(p_strMarkerStart, intPrevMarkerEndPos)
	if(intPrevMarkerStartPos != -1)
	{
		editor.setSelectedRange(intPrevMarkerStartPos + p_strMarkerStart.length, 0);
		editor.activate();
		return true;
	}
	else
	{
		editor.activate();
		return false;
	}
}

cursorMoveAfterPrevious("[", "]");

If you add these to your own actions and keyboard shortcuts, you should then be able to move forwards/backwards, and trigger dictation based on whether the cursor moved or not.

Hope that helps.

Thank you so much! That worked great.