HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="game">
<div class="intro">
<h1>Welcome to accessible Sudoku</h1>
<p>This is a version of Sudoku designed to work with a screenreader like NVDA or JAWS. An approximation of the output of a screenreader is also shown below the game board along with the ability to toggle a simulation of partial sight so non-screenreader users can get an idea of what it is like to use the game with a screenreader.</p>
<div class="help" id="help"></div>
<p><a href="#in-content-skip-link-target-post-board" class="text-assistive display-in-place-on-focus">Skip past game board.</a> <a href="#in-content-skip-link-target-pre-board" class="text-assistive display-in-place-on-focus" id="in-content-skip-link-target-pre-board">Sudoku game board starts below.</a></p>
</div>
<div class="board blur">
<div data-id="A" class="square">
<input id="11" data-id="A" aria-label="Square A, Cell A." data-correct="1" type="number" min="0" max="4" length="1" value="">
<input id="12" data-id="B" aria-label="Square A, Cell B. The value of this cell is fixed." data-correct="2" type="number" min="0" max="4" value="2" class="readonly">
<input id="21" data-id="C" aria-label="Square A, Cell C." data-correct="4" type="number" min="0" max="4" value="">
<input id="22" data-id="D" aria-label="Square A, Cell D." data-correct="3" type="number" min="0" max="4" value="">
</div>
<div data-id="B" class="square">
<input id="13" data-id="A" aria-label="Square B, Cell A. The value of this cell is fixed." data-correct="3" type="number" min="0" max="4" value="3" class="readonly">
<input id="14" data-id="B" aria-label="Square B, Cell B." data-correct="4" type="number" min="0" max="4" value="">
<input id="23" data-id="C" aria-label="Square B, Cell C." data-correct="2" type="number" min="0" max="4" value="">
<input id="24" data-id="D" aria-label="Square B, Cell D. The value of this cell is fixed." data-correct="1" type="number" min="0" max="4" value="1" class="readonly">
</div>
<div data-id="C" class="square">
<input id="31" data-id="A" aria-label="Square C, Cell A. The value of this cell is fixed." data-correct="2" type="number" min="0" max="4" value="2" class="readonly">
<input id="32" data-id="B" aria-label="Square C, Cell B." data-correct="1" type="number" min="0" max="4" value="">
<input id="41" data-id="C" aria-label="Square C, Cell C." data-correct="3" type="number" min="0" max="4" value="">
<input id="42" data-id="D" aria-label="Square C, Cell D. The value of this cell is fixed." data-correct="4" type="number" min="0" max="4" value="4" class="readonly">
</div>
<div data-id="D" class="square">
<input id="33" data-id="A" aria-label="Square D, Cell A." data-correct="4" type="number" min="0" max="4" value="">
<input id="34" data-id="B" aria-label="Square D, Cell B." data-correct="3" type="number" min="0" max="4" value="">
<input id="43" data-id="C" aria-label="Square D, Cell C. The value of this cell is fixed." data-correct="1" type="number" min="0" max="4" value="1" class="readonly">
<input id="44" data-id="D" aria-label="Square D, Cell D." data-correct="2" type="number" min="0" max="4" value="">
</div>
</div>
<div class="controls">
<label><input id="highlight" type="checkbox"> HIGHLIGHT (CH)</label> <button id="check">CHECK YOUR ANSWER (CC)</button> <button id="show">SHOW SOLUTION (CS)</button> <button id="restart">RESTART (CR)</button>
<br><label><input id="hide" type="checkbox"> SIMULATE FULL LOSS OF VISION (CF)</label> <label><input id="blur" type="checkbox" checked> SIMULATE PARTIAL SIGHT (CP)</label> <label><input id="simulate-sr" type="checkbox" checked> SHOW SCREENREADER OUTPUT (CO)</label>
</div>
<div class="announcement">
<div class="sr-simulation">
<h2 aria-hidden="true">Simulated screenreader output</h2>
<div id="sr-simulation-text" class="message-aria" aria-hidden="true"></div>
<div id="announcement-sr-only" aria-live="polite"></div>
</div>
<div id="announcement-all-users" aria-live="polite"></div>
<div id="highlight-help" class="v-hidden highlight-help"><span class="correct"></span> = correct <span class="incorrect"></span> = incorrect</div>
</div>
</div>
<div class="post-game">
<p><a href="#in-content-skip-link-target-pre-board" class="text-assistive display-in-place-on-focus">Skip back to game board.</a> <a href="#in-content-skip-link-target-post-board" class="text-assistive display-in-place-on-focus" id="in-content-skip-link-target-post-board">Continue reading post sudoku game board.</a></p>
<h2>Credits & Licence</h2>
<p>Designed by Paul Ratcliffe: find me <a href="https://ko-fi.com/puzzletrail" target="_blank" rel="noopener noreferrer">on Ko-fi</a>, <a href="https://twitter.com/PuzzleTrailPaul" target="_blank" rel="noopener noreferrer">on Twitter</a> or <a href="https://www.linkedin.com/in/paulrratcliffe/" target="_blank" rel="noopener noreferrer">on LinkedIn</a>. I'd love to know what you think of this and how it can be improved, so please get in touch!</p>
<p>Code is available under the <a href="https://opensource.org/licenses/MIT" target="_blank" rel="noopener noreferrer">MIT license</a>, copyright Paul Ratcliffe 2022.</p>
<p><a href="#in-content-skip-link-target-pre-board" class="text-assistive display-in-place-on-focus">Skip back to game board.</a></p>
<p> </p>
<p> </p>
<p> </p>
</div>
<!--
Copyright 2022 Paul Ratcliffe
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
/* See https://davidmathlogic.com/colorblind/ */
*, *:before, *:after {
box-sizing: border-box;
}
body {
font-size: 1.25rem;
line-height: 1.35;
}
li {
padding-bottom: 0.65rem;
}
.v-hidden {
visibility: hidden;
}
.game {
text-align: center;
}
.intro {
max-width: 60rem;
margin: 0 auto;
text-align: left;
}
.board {
width: 40vh;
max-width: 90vw;
height: 40vh;
max-height: 90vw;
margin: 2rem auto 2rem auto;
padding: 0;
border: 2px solid black;
display: grid;
grid-template: repeat(2,1fr) / repeat(2,1fr);
}
.board.blur {
filter: blur(3vh);
}
.board.hide {
opacity: 0;
}
.square {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
border: 2px solid black;
display: grid;
grid-template: repeat(2,1fr) / repeat(2,1fr);
}
.square input {
display: block;
color: black;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
text-align: center;
font-size: 7vh;
border: 1px solid black;
-moz-appearance: textfield;
}
/* Hide HTML5 Up and Down arrows. */
.square input::-webkit-outer-spin-button, .square input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
.correct {
background-color: #56B4E9;
}
.incorrect {
background-color: #CC79A7;
}
.square input:focus {
background-color: #F0E442;
}
.square input:read-only {
background-color: #333;
color: white;
}
.square input.readonly {
background-color: #333;
color: white;
}
.controls {
font-size: 1.35rem;
max-width: 60rem;
margin: 1rem auto;
line-height: 1.75;
}
.controls label {
margin: 0 2rem;
white-space: nowrap;
display: inline-block;
}
.controls input[type="checkbox"] {
width: 1.375rem;
height: 1.375rem;
display: inline;
}
.controls button {
font-size: 1.35rem;
margin: 0 2rem;
color: black;
background-color: lightgrey;
border: 1px solid darkgrey;
}
.controls button:hover, .controls button:focus {
color: white;
background-color: black;
border: 1px solid lightgrey;
}
.announcement {
max-width: 60rem;
margin: 1.75rem auto 2rem auto;
text-align: left;
font-size: 1.675rem;
}
.announcement div {
margin: 1rem 0;
}
.sr-simulation {
border: 2px solid black;
border-radius: 5px;
background-color: #FFFFE6;
padding: 0 1rem;
}
.sr-simulation h2 {
font-size: 1rem;
}
.sr-simulation.hide {
-webkit-clip-path: polygon(0 0, 0 0, 0 0, 0 0);
clip-path: polygon(0 0, 0 0, 0 0, 0 0);
box-sizing: border-box;
position: absolute;
margin: 0;
padding: 0;
}
.highlight-help {
font-size: 1.675rem;
}
.highlight-help span {
display: inline-block;
height: 2rem;
width: 2rem;
}
.post-game {
max-width: 60rem;
margin: 0 auto;
text-align: left;
}
/* Skip link styling */
.text-assistive {
-webkit-clip-path: polygon(0 0, 0 0, 0 0, 0 0);
clip-path: polygon(0 0, 0 0, 0 0, 0 0);
box-sizing: border-box;
position: absolute;
margin: 0;
padding: 0;
}
#skip-link-target:focus {
background: #084367;
color: white;
}
.text-assistive.display-in-place-on-focus {
display: inline-block;
overflow: hidden;
transform: translateX(-200vw);
}
.text-assistive.display-in-place-on-focus:focus {
-webkit-clip-path: none;
clip-path: none;
z-index: 999;
padding: 0 0.5rem;
transform: translateX(0);
background: #1295e6;
text-decoration: none;
color: white;
}
/* end of skip link styling */
/* improved keyboard focus visibility */
a:focus {
border-bottom-color: #1295e6;
}
/* end of improved keyboard focus visibility */
/*
Copyright 2022 Paul Ratcliffe
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
Copyright 2022 Paul Ratcliffe
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const size = 4;
let lang = "en";
const messages = {
en: {
cellValue: "The value of this cell is ",
cellEmpty: "This cell is empty.",
cellEmptyShort: "Empty",
cellCorrect: "This cell is correct",
cellCorrectShort: "correct",
cellIncorrect: "This cell is incorrect",
cellIncorrectShort: "incorrect",
cellFixedShort: "fixed value",
boardCorrect: "Congratulations, you completed the Sudoku correctly!",
boardIncorrect: "Hmmm... Not quite correct! Try again. Check 'HIGHLIGHT' to see where you went wrong.",
where: "You are currently in ",
currentRow: "current row",
currentColumn: "current column",
currentSquare: "square",
reading: "Reading",
help: `
<div class="welcome-message">
<h2>How to play</h2>
<p>Note: this version of the game has the partial vision simulation on by default. You can turn it off using the controls below the board or by using the keyboard shortcuts explained below. Other than this simulation, mouse users can use the game just as any other online sudoku game.
<p>This is a simpler version of sudoku that uses just the numbers 1 to 4 rather than 1 to 9.
<p>The game BOARD consists of 4 SQUARES arranged in a 2 by 2 grid. The SQUARES in the top row are designated SQUARE A and SQUARE B. The SQUARES in the bottom row are designated SQUARE C and SQUARE D.
<p>Each SQUARE contains 4 CELLS arranged in a 2 by 2 grid. The CELLS in the top row of each SQUARE are designated CELL A and CELL B. The CELLS in the bottom row are designated CELL C and CELL D.
<p>Keyboard users can use TAB and SHIFT+TAB to move through each of the cells one by one in SQUARE, then CELL order. So, as you tab you will travel through all 4 CELLS of SQUARE A, then all 4 CELLS of SQUARE B, etc.
<p>If you are using a screenreader or can see the simulated screenreader output, you can tell which SQUARE and which CELL you are in at all times, as well as the current value of the cell.
<p>You can use keyboard shortcuts to move quickly around the board and also to have the screenreader announce the other numbers that are entered in your current SQUARE, ROW or COLUMN. Keyboard shortcuts are only active whilst the keyboard focus is within the game. The keyboard shortcuts are:
<ul>
<li>W: Give details of current cell.
<li>RR: Read current ROW.
<li>RC: Read current COLUMN.
<li>RS: Read current SQUARE.
<li>Cursor keys: Go to adjacent cell in indicated direction.
<li>G[SQUARE][CELL]: Go to chosen cell. E.g. GBC will move the keyboard focus to SQUARE B, CELL C.
<li>CC: Check your answers. This will tell you if the sudoku is completely solved or not.
<li>CH: Toggle highlighting which cells are correct and which are incorrect.
<li>CS: Reveal solution.
<li>CR: Restart game.
<li>CF: Toggle simulating full loss of vision by hiding the board completely.
<li>CP: Toggle simulating partial sight by blurring the board. This is on by default.
<li>CO: Toggle displaying simulated screenreader output below the board.
</ul>
</div>
`
},
announcementScreenreaderOnlyDisplayed: false,
announcementAllUsersDisplayed: false
}
let keyboardNavCapture = 0;
let capturedKeys = "";
document.querySelector('#help').innerHTML = messages[lang].help;
document.querySelectorAll('.square input').forEach( (cell) => {
cell.addEventListener('focus', (e) => {
let cellValue = e.target.value;
let cellMessage = " " + messages[lang].cellValue + " " + cellValue + ".";
if(cellValue == ""){
cellMessage = " " + messages[lang].cellEmpty;
}
document.querySelector('#sr-simulation-text').innerHTML = e.target.ariaLabel + cellMessage;
} );
cell.addEventListener('blur', (e) => {
document.querySelector("#announcement-sr-only").innerText = "";
if(e.target.classList.contains('readonly')){
e.target.value = e.target.dataset.correct;
} else {
let currentValue = parseInt(e.target.value);
if(Number.isNaN(currentValue)) {
currentValue = 0;
}
if(currentValue < 1 || currentValue > size){
e.target.value = "";
}
}
document.querySelector('#sr-simulation-text').innerHTML = "";
if(messages.announcementScreenreaderOnlyDisplayed){
document.querySelector('#announcement-sr-only').innerHTML = "";
messages.announcementScreenreaderOnlyDisplayed = false;
}
if(messages.announcementAllUsersDisplayed){
document.querySelector('#announcement-all-users').innerHTML = "";
messages.announcementAllUsersDisplayed = false;
}
if(document.querySelector('#highlight').checked){
let ariaLabelSplit = e.target.ariaLabel.split('.');
if(e.target.value === e.target.dataset.correct){
e.target.classList.add("correct");
e.target.classList.remove("incorrect");
e.target.ariaLabel = ariaLabelSplit[0] + ". " + messages[lang].cellCorrect;
} else {
e.target.classList.add("incorrect");
e.target.classList.remove("correct");
e.target.ariaLabel = ariaLabelSplit[0] + ". " + messages[lang].cellIncorrect;
}
}
} );
} );
function checkAnswer(e) {
let allCorrect = true;
document.querySelectorAll('.square input').forEach( (cell) => {
if( cell.value !== cell.dataset.correct){
allCorrect = false;
}
} );
if(allCorrect){
document.querySelector('#announcement-all-users').innerHTML = messages[lang].boardCorrect;
} else {
document.querySelector('#announcement-all-users').innerHTML = messages[lang].boardIncorrect;
}
messages.announcementAllUsersDisplayed = true;
}
document.querySelector('#check').addEventListener('click', checkAnswer );
function showSolution(e){
if(messages.announcementScreenreaderOnlyDisplayed){
document.querySelector('#announcement-sr-only').innerHTML = "";
messages.announcementScreenreaderOnlyDisplayed = false;
}
if(messages.announcementAllUsersDisplayed){
document.querySelector('#announcement-all-users').innerHTML = "";
messages.announcementAllUsersDisplayed = false;
}
document.querySelectorAll('.square input').forEach( (cell) => {
cell.value = cell.dataset.correct;
} );
}
document.querySelector('#show').addEventListener('click', showSolution );
function restartGame(e){
if(messages.announcementScreenreaderOnlyDisplayed){
document.querySelector('#announcement-sr-only').innerHTML = "";
messages.announcementScreenreaderOnlyDisplayed = false;
}
if(messages.announcementAllUsersDisplayed){
document.querySelector('#announcement-all-users').innerHTML = "";
messages.announcementAllUsersDisplayed = false;
}
document.querySelectorAll('.square input:not(.readonly)').forEach( (cell) => {
cell.value = "";
} );
}
document.querySelector('#restart').addEventListener('click', restartGame );
function highlightCells(e){
if(messages.announcementScreenreaderOnlyDisplayed){
document.querySelector('#announcement-sr-only').innerHTML = "";
messages.announcementScreenreaderOnlyDisplayed = false;
}
if(document.querySelector('#highlight').checked){
document.querySelector('#highlight-help').classList.remove('v-hidden');
} else {
document.querySelector('#highlight-help').classList.add('v-hidden');
}
document.querySelectorAll('.square input:not(.readonly)').forEach( (cell) => {
let ariaLabelSplit = cell.ariaLabel.split('.');
if(document.querySelector('#highlight').checked){
if(cell.value === cell.dataset.correct){
cell.classList.add("correct");
cell.classList.remove("incorrect");
cell.ariaLabel = ariaLabelSplit[0] + ". " + messages[lang].cellCorrect;
} else {
cell.classList.add("incorrect");
cell.classList.remove("correct");
cell.ariaLabel = ariaLabelSplit[0] + ". " + messages[lang].cellIncorrect;
}
} else {
cell.classList.remove("correct","incorrect");
cell.ariaLabel = ariaLabelSplit[0];
}
} );
}
document.querySelector('#highlight').addEventListener('change', highlightCells );
function blurBoard(e){
if(document.querySelector('#blur').checked){
document.querySelector('.board').classList.add('blur');
} else {
document.querySelector('.board').classList.remove('blur');
}
}
document.querySelector('#blur').addEventListener('change', blurBoard );
function hideBoard(e){
if(document.querySelector('#hide').checked){
document.querySelector('.board').classList.add('hide');
} else {
document.querySelector('.board').classList.remove('hide');
}
}
document.querySelector('#hide').addEventListener('change', hideBoard );
function showScreenreaderSimulation(e){
if(document.querySelector('#simulate-sr').checked){
document.querySelector('.sr-simulation').classList.remove('hide');
} else {
document.querySelector('.sr-simulation').classList.add('hide');
}
}
document.querySelector('#simulate-sr').addEventListener('change', showScreenreaderSimulation );
document.querySelector('.board').addEventListener('keydown', (e) => {
if (e.defaultPrevented) {
return; // Do nothing if the event was already processed
}
if(e.key === "Escape"){
keyboardNavCapture = 0;
capturedKeys = "";
} else if(e.key.length > 5 && e.key.substring(0,5) === "Arrow"){
keyboardNavCapture = 0;
capturedKeys = "";
e.preventDefault();
let activeCellId = parseInt(document.activeElement.id);
if(!Number.isNaN(activeCellId)) {
let targetCellId = 0;
switch (e.key) {
case "ArrowUp":
targetCellId = activeCellId - 10;
break;
case "ArrowRight":
targetCellId = activeCellId + 1;
break;
case "ArrowDown":
targetCellId = activeCellId + 10;
break;
case "ArrowLeft":
targetCellId = activeCellId - 1;
break;
}
if(targetCellId > 10 && targetCellId < ((size * 11) + 1) ) {
if(document.getElementById('' + targetCellId)){
document.getElementById('' + targetCellId).focus();
document.getElementById('' + targetCellId).select();
}
}
}
} else if(keyboardNavCapture === 0){
if(e.key === "g" || e.key === "G"){ // GO action. Capture the next two key presses to determine where to go
keyboardNavCapture = 2;
capturedKeys = "G";
e.preventDefault();
} else if(e.key === "r" || e.key === "R"){ // READ action. Capture the next key press to determine what to read
keyboardNavCapture = 1;
capturedKeys = "R";
e.preventDefault();
} else if(e.key === "c" || e.key === "C"){ // CONTROL action. Capture the next key press to determine which control action
keyboardNavCapture = 1;
capturedKeys = "C";
e.preventDefault();
} else if(e.key === "w" || e.key === "W"){ // WHERE action. Carry out immediately
let announcementScreenreaderOnly = messages[lang].where + document.activeElement.ariaLabel;
cellValue = document.activeElement.value;
if(cellValue === ""){
cellValue = messages[lang].cellEmptyShort;
} else if(document.activeElement.classList.contains('readonly')){
cellValue += " (" + messages[lang].cellFixedShort + ")";
} else if(document.querySelector('#highlight').checked){
if(cellValue === document.activeElement.dataset.correct){
cellValue += " (" + messages[lang].cellCorrectShort + ")";
} else {
cellValue += " (" + messages[lang].cellIncorrectShort + ")";
}
}
document.querySelector("#announcement-sr-only").innerText = announcementScreenreaderOnly + " " + messages[lang].cellValue + cellValue + ".";
e.preventDefault();
} else if(e.key === "h" || e.key === "H"){ // HELP action. Carry out immediately
document.querySelector("#announcement-sr-only").innerHTML = messages[lang].help;
e.preventDefault();
}
} else {
capturedKeys += e.key.toUpperCase();
keyboardNavCapture = keyboardNavCapture - 1;
e.preventDefault();
if(keyboardNavCapture == 0){
let actionType = capturedKeys[0];
let actionDetails = capturedKeys.substring(1);
capturedKeys = "";
if(actionType === "R"){ // READ action
let activeCellId = parseInt(document.activeElement.id);
if( (actionDetails === "R" || actionDetails === "C" || actionDetails === "S") && !Number.isNaN(activeCellId) ){
let row = parseInt(document.activeElement.id[0]) * 10;
let column = parseInt(document.activeElement.id[1]);
let cellValue = "";
let announcementScreenreaderOnly = messages[lang].reading + " ";
switch (actionDetails){
case "R":
announcementScreenreaderOnly += messages[lang].currentRow + ":";
for (let col = 1; col < (size + 1); col++) {
cellValue = document.getElementById("" + (row + col)).value;
if(cellValue === ""){
cellValue = messages[lang].cellEmptyShort;
} else if(document.getElementById("" + (row + col)).classList.contains('readonly')){
cellValue += " (" + messages[lang].cellFixedShort + ")";
} else if(document.querySelector('#highlight').checked){
if(cellValue === document.getElementById("" + (row + col)).dataset.correct){
cellValue += " (" + messages[lang].cellCorrectShort + ")";
} else {
cellValue += " (" + messages[lang].cellIncorrectShort + ")";
}
}
announcementScreenreaderOnly += " " + cellValue;
if(col < size){
announcementScreenreaderOnly += ",";
}
}
break;
case "C":
announcementScreenreaderOnly += messages[lang].currentColumn + ":";
for (let r = 10; r < ((size + 1) * 10); r = r+10) {
cellValue = document.getElementById("" + (r + column)).value;
if(cellValue === ""){
cellValue = messages[lang].cellEmptyShort;
} else if(document.getElementById("" + (r + column)).classList.contains('readonly')){
cellValue += " (" + messages[lang].cellFixedShort + ")";
} else if(document.querySelector('#highlight').checked){
if(cellValue === document.getElementById("" + (r + column)).dataset.correct){
cellValue += " (" + messages[lang].cellCorrectShort + ")";
} else {
cellValue += " (" + messages[lang].cellIncorrectShort + ")";
}
}
announcementScreenreaderOnly += " " + cellValue;
if(r < size * 10){
announcementScreenreaderOnly += ",";
}
}
break;
case "S":
let square = document.activeElement.parentNode;
announcementScreenreaderOnly += messages[lang].currentSquare + " " + square.dataset.id + ":";
for (let cell = 65; cell < (size + 65); cell++) {
cellValue = square.querySelector('input[data-id="' + String.fromCharCode(cell) + '"]').value;
if(cellValue === ""){
cellValue = messages[lang].cellEmptyShort;
} else if(square.querySelector('input[data-id="' + String.fromCharCode(cell) + '"]').classList.contains('readonly')){
cellValue += " (" + messages[lang].cellFixedShort + ")";
} else if(document.querySelector('#highlight').checked){
if(cellValue === square.querySelector('input[data-id="' + String.fromCharCode(cell) + '"]').dataset.correct){
cellValue += " (" + messages[lang].cellCorrectShort + ")";
} else {
cellValue += " (" + messages[lang].cellIncorrectShort + ")";
}
}
announcementScreenreaderOnly += " " + cellValue;
if(cell < (size + 64) ){
announcementScreenreaderOnly += ",";
}
}
break;
}
document.querySelector("#announcement-sr-only").innerText = announcementScreenreaderOnly;
}
}
if(actionType === "G"){ // GO action
if( (actionDetails.charCodeAt(0) - 65) < size && (actionDetails.charCodeAt(1) - 65) < size ){
let targetCell = document.querySelector('.square[data-id="' + actionDetails[0] + '"] input[data-id="' + actionDetails[1] + '"]');
if(targetCell){
targetCell.focus();
targetCell.select();
}
}
}
if(actionType === "C"){
switch (actionDetails){
case "F":
if(document.querySelector('#hide').checked){
document.querySelector('#hide').checked = false;
} else {
document.querySelector('#hide').checked = true;
}
hideBoard(null);
break;
case "P":
if(document.querySelector('#blur').checked){
document.querySelector('#blur').checked = false;
} else {
document.querySelector('#blur').checked = true;
}
blurBoard(null);
break;
case "O":
if(document.querySelector('#simulate-sr').checked){
document.querySelector('#simulate-sr').checked = false;
} else {
document.querySelector('#simulate-sr').checked = true;
}
showScreenreaderSimulation(null);
break;
case "H":
if(document.querySelector('#highlight').checked){
document.querySelector('#highlight').checked = false;
} else {
document.querySelector('#highlight').checked = true;
}
highlightCells(null);
break;
case "C":
checkAnswer(null);
break;
case "S":
showSolution(null);
break;
case "R":
restartGame(null);
break;
}
}
}
}
});
Also see: Tab Triggers