So, When I press the arrow keys on the keyboard (left, right, up, down) the green hover color goes to the corresponding button.
What I want to do, is every time I press the "return" or "enter" key (keycode 13), to write the corresponding number on the screen.
For example, if the green hover color is 2 and I prees the "Return/Enter" key, write "Two" on the screen. I want the same thing to happen with the mouse. For example, if I click on 2 , write "Two" on the screen.
How can I achieve this? And of course a better coding in all of this is always welcome.. Thanks a lot!!!
var columns = 0;
var rowButtons = 0;
var $rows = $('.multiple');
var buttonSelected;
var arrows = { left: 37, up: 38, right: 39, down: 40, enter: 13 };
$(window).keydown(function(e) {
if (Object.values(arrows).indexOf(e.which) > -1) {
e.preventDefault();
$('.multiple button').removeClass('selected');
switch (e.which) {
case arrows.up:
rowButtons = rowButtons == 0 ? $rows.length - 1 : rowButtons - 1;
break;
case arrows.down:
rowButtons = rowButtons == $rows.length - 1 ? 0 : rowButtons + 1;
break;
case arrows.left:
$buttonsInRow = $('.multiple:eq(' + rowButtons + ') button');
columns = columns == 0 ? $buttonsInRow.length - 1 : columns - 1;
break;
case arrows.right:
$buttonsInRow = $('.multiple:eq(' + rowButtons + ') button');
columns = columns == $buttonsInRow.length - 1 ? 0 : columns + 1;
break;
case arrows.enter:
rowButtons = rowButtons == $rows.click;
document.getElementById("screen").innerHTML ="one"
break;
}
buttonSelected = $('.multiple:eq(' + rowButtons + ') button:eq(' + columns + ')');
buttonSelected.addClass('selected');
}
});
#screen {
width:100%;
padding:20px;
background: #222;
color:lime;
}
button.selected {
background: green
}
.multiple button {
border: 1px solid blue;
color: #222;
padding: 10px 25px;
cursor: pointer;
width:25%;
}
.multiple button:hover {
background-color: orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="screen">--</button>
<div class="multiple">
<button>1</button>
<button>2</button>
<button>3</button>
</div>
<div class="multiple">
<button>4</button>
<button>5</button>
<button>6</button>
</div>
<div class="multiple">
<button>7</button>
<button>8</button>
<button>9</button>
</div>