[Mobile] Detect when typing with a softkeyboard versus physical keyboard

Disclaimer

Is this project open source? Yes
Is this project completely free? Yes
Is this project vibe-coded beyond the author’s ability to comprehend how it works? No

Save this as a .js file and point CodeScript ToolKit to it as your “Startup script path.” It listens to the softkeyboard being shown or hidden as well if any key is pressed[1]. If one of those things happen, it’ll check if a physical keyboard is connected. If a physical keyboard was connected and wasn’t connected during the last check, it’ll show the notice “Hello, keyboard!” If a keyboard was disconnected and was connected during the last check, it’ll say “Goodbye, keyboard!”

exports.invoke = async (app) => {
	let storedCapacitorPhysicalKeyboard = {hasPhysicalKeyboard: null};
	
	async function testPhysicalKeyboard() {
		let testCapacitorPhysicalKeyboard = await window.Capacitor?.nativePromise('Keyboard', 'hasPhysicalKeyboard');

		if (testCapacitorPhysicalKeyboard.hasPhysicalKeyboard == storedCapacitorPhysicalKeyboard.hasPhysicalKeyboard) return;
		storedCapacitorPhysicalKeyboard = testCapacitorPhysicalKeyboard;

		if (testCapacitorPhysicalKeyboard.hasPhysicalKeyboard) {
			new Notice("Hello, keyboard!");
		} else {
			new Notice("Goodbye, keyboard!");
		}
	}
	
	const onKeyDown = () => {
		testPhysicalKeyboard();
		window.removeEventListener('keydown', onKeyDown);
	}

	if (app.isMobile) {
		testPhysicalKeyboard();

		window.addEventListener('keyboardDidShow', async () => {
			testPhysicalKeyboard();
			window.removeEventListener('keydown', onKeyDown);
		});

		window.addEventListener('keyboardDidHide', async () => {
			testPhysicalKeyboard();
			window.removeEventListener('keydown', onKeyDown);
			window.addEventListener('keydown', onKeyDown);
		});

		window.addEventListener('keydown', onKeyDown);
	}
}

Why? To do something like this:
On ipad, is there a way to execute a command when external keyboard is de/activated - Help - Obsidian Forum

Though, it’s not instant if the softkeyboard was not visible while connecting or disconnecting the physical keyboard. You either have to press a key on the physical keyboard to tell it you’re using the physical keyboard or open up the softkeyboard[2] to tell it you’re using the softkeyboard.

Running whatever command you want

To get the command IDs for every command palette command, run
console.log(Object.keys(app.commands.commands))
then, in your script, you can use it like
app.commands.executeCommandById('workspace:edit-file-title')[3]. Just replace new Notice("Hello, keyboard!") and/or new Notice("Goodbye, keyboard!").


  1. Only if the softkeyboard is hidden. Also, it stops listening for keys after it “hears” one. It’ll start listening again if the softkeyboard is shown and hidden again. ↩︎

  2. If you’re focused on a text field when disconnecting the physical keyboard, Obsidian should automatically open the softkeyboard, anyway. ↩︎

  3. This goes into editing the current file’s title. ↩︎

1 Like

I’ve edited this script since first posting it around two hours ago. Most notably, I made it so that it doesn’t listen for any key if it doesn’t need to since that seemed like it could cause performance issues. Now, it stops listening for keys when the softkeyboard is visible and restarts listening when the softkeyboard is hidden. It also stops listening to keys after it “hears” one key.

So…

Softkeyboard visible

  • Not listening for keys
  • Will realize the physical keyboard is connected by the fact that connecting it closes the softkeyboard

Softkeyboard hidden

  • Listening for keys
  • Will realize the physical keyboard is connected when you press any key on it and then stop listening for keys

When not listening for keys

This happens when either of these things happen:

  1. The softkeyboard is visible
    • Plugging in the physical keyboard closes the softkeyboard, which then triggers the script to check and realize the keyboard is connected
  2. A key is pressed
    • If it already realized you pressed the key on the physical keyboard, it doesn’t need to keep realizing that… Until you switch back to typing with the softkeyboard[1]

  1. Go to the “Softkeyboard visible” section. ↩︎

@maciej1
To get command IDs without console, have CodeScript Toolkit installed and put this in a note:

```code-button
---
caption: 'All command IDs'
---
console.log(Object.keys(app.commands.commands))
```

then tap the “All command IDs” button.

Typewriter Mode’s enable typewriter scolling

typewriter-mode:typewriter-enable

So, the JS bit would be:

app.commands.executeCommandById('typewriter-mode:typewriter-enable');
Typewriter Mode’s disable typewriter scolling

typewriter-mode:typewriter-disable

JS bit would be:

app.commands.executeCommandById('typewriter-mode:typewriter-disable');
1 Like

@Abisheik that is truly awesome, thank you so much!

1 Like