How to detect when cursor is within a table cell in Microsoft OneNote?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
GrantSRobertson
Posts: 7
Joined: 04 May 2022, 15:57

How to detect when cursor is within a table cell in Microsoft OneNote?

Post by GrantSRobertson » 26 May 2022, 17:51

I use a single-cell table in OneNote to "hold" quotes. I find it is the best way to contain a quote and maintain its formatting (even if multiple lines or with indented paragraphs, etc.), compared to simply pasting the quote "directly" onto the OneNote page. When pasting multiple, separate quotes, OneNote will leave a small gap between each separate single-cell table. I like this because this makes it clear that the quotes are separate entities. Unfortunately, when moving these separate table cells around, OneNote has a tendency to stick the cells together. I have written a couple of hotkeys to allow me to manipulate those "conjoined" table cells into separate single-cell tables. They work just fine if the cursor is actually inside a table cell. However, if the cursor is not within a table cell, it will delete either a single character or an entire paragraph. I have written the script such that OneNote will beep at me if this happens. However, I would like to make the script even more perfect in that it simply will not do anything when the hotkey is used in a regular paragraph.

So.... I am looking for some trick to detect when the cursor is (or is not) inside of a table cell. I have already examined the hell out of the control names and window text using Window Spy. I can't find anything that is different depending on whether in a cell or not. In fact, OneNote seems to be a bit of an odd fish, in that the individual controls don't show up in Window Spy, only larger groupings of controls. So, looking for the Table tab will not work, as it is buried inside a larger control.

Yes, I know this is a stretch. But.... If you have any ideas, let me know.

For those interested, here is the pertinent code:

Code: Select all

; Insert blank paragraph below current quote (single-cell table).
; WARNING: If you use this hotkey on a regular paragraph, it will cause OneNote to beep at you
; then BACKSPACE OVER ONE CHARACTER. Press Ctrl-z to correct.
; I am working on figuring out a way to detect whether the cursor was in a table cell or if there
; was any text selected after attempting to select the table cell.
; Currently, you just have to only use it when in a table cell.
F22 & F13 Up::
	onenote_insert_blank_paragraph_below_current_quote(ThisHotkey)
	{
		Keywait "F22"

		; Select the entire table cell.
		Send "{Alt down}{Alt up}jlo"

		; Move cursor to end of cell.
		Send "{End}"

		; Create a new table row beneath it.
		Send "{Ctrl down}{Enter}{Ctrl up}"

		; Convert that back to a regular paragraph.
		Send "{Backspace}"
	}

; Separate two "conjoined" quotes, leaving a non-paragraph gap. Cursor must be in top quote.
; WARNING: If you use this hotkey on a regular paragraph, It will cause OneNote to beep at you,
; then it DELETES THE REGULAR PARAGRAPH or DELETES ONE LINE of the above table.
; This may require multiple presses of Ctrl+z to correct.
F22 & F14 Up::
{
	onenote_insert_blank_paragraph_below_current_quote(ThisHotkey)

	; Remove that blank line in such a way that it leaves table rows separated.
	; This also leaves cursor in the subsequent table cell, so user can just keep pressing this key
	; to continue separating a list of quotes that got conjoined by moving them around.
	; Also has no effect when the bottom paragraph is just a regular paragraph.
	Send "{Ctrl down}a{Ctrl up}{Delete}"
}

{NOTE: Using the F22 & F13 as my hotkeys is part of how I make use of the extra 18 macro keys on my Corsair K95W keyboard. I will write an entirely separate thread on how I do all that later... much later. I use the 'Up' keyword and the 'Keywait' command because I want to ensure that those keys are fully released before starting in on the hotkey script.}

Versions, Etcetera:
AutoHotkey v2.0-beta.3
Microsoft® OneNote® 2016 MSO (Version 2204 Build 16.0.15128.20210) 32-bit

Return to “Ask for Help (v2)”