const buttons = document.querySelectorAll('[tabindex="0"]');
let selected = 0;
let absoluteMode = false;

document.body.addEventListener("keydown", event => {
	if(absoluteMode) {
		if(event.key == "*") {
			absoluteMode = !absoluteMode;
		} else {
			let key = parseInt(event.key);
			if(key >= 0 && key <= 9) {
				buttons[(key + 10) % 11].click();
			}
		}
	} else {
		switch(event.key) {
			case "6":
			case "k":
			case "K":
				if(selected > 0)
					selected--;
				buttons[selected].focus();
				break;
			case "9":
			case "j":
			case "J":
				if(selected < buttons.length)
					selected++;
				buttons[selected].focus();
				break;
			case "8":
				buttons[selected].click();
				break;
			case "*":
				absoluteMode = !absoluteMode;
				break;
		}
	}
});