In Mathematica, it is possible to create a Table
or Grid
of \[Placeholder]
boxes. For example, I can make a 3-by-3 Grid
of \[Placeholder]
boxes:
Grid[
Table[\[Placeholder], {3}, {3}],
Frame -> All, ItemSize -> Full]
Then, I can click on one of the placeholders (for example, the top left-hand placeholder) to change focus to it:
Once focus is on one of the placeholders, I can use the arrow keys on the keyboard or the Tab key to change the focus to one of the other placeholders (at least in Windows XP; I have not tested this on later versions of Windows or Mac or Linux).
Using the Tab key is particularly handy, because it cycles through the placeholders in the "canonical" order (i.e., cells {1,1}, {1,2}, {1,3}, {2,1}, {2,2}, {2,3}, {3,1}, {3,2}, {3,3} where the elements are {row index, column index}):
But, it can somewhat of a chore to cycle through the placeholders using Tab, especially for large n-by-n grids. My question is, is it possible to interactively change focus in a grid using the keyboard?
For example, suppose that I label the rows with letters and the columns with numbers:
TableForm[
Table[\[Placeholder], {3}, {3}],
TableHeadings -> {{"a", "b", "c"}, {"1", "2", "3"}}]
Is there any way to write a function in which I could type "b2", for example, using the keyboard and have Mathematica automatically change focus to the placeholder at coordinate b2, like the following?
With the focus now at "b2", I can mark this coordinate it in some way, perhaps by typing an "x":
Remember the board game Battleship? It is basically a game of placing "x" at "hit" coordinates (and placing nothing at "miss" coordinates):
I would like to use Mathematica to specify and mark coordinates in a grid interactively, using the keyboard. It doesn't matter how the coordinates are marked. In other words, I don't necessarily need the script to change \[Placeholder]
focus in a Grid
; I just need to be able to specify coordinates such as "b2" interactively (i.e., in real time, not from a list of predefined coordinates) and be able to mark those coordinates in some way. So, in this sense, it doesn't necessarily need to be a Grid
of \[Placeholder]
s. It could be a Grid
of Checkbox
es. It could be a Grid
of Rectangle
s that change color when specified using the keyboard. In other words, the important feature I am looking for is to use the keyboard to specify coordinates, not mouse clicks.
Do you have any initial thoughts of how I can accomplish this? Do I need to use Input
or InputField
, or are there other ways to get keyboard input from the user? Thanks so much for your time and help.