How to use arrow keys to select squares in a grid
in
Programming Questions
•
5 months ago
So I've made a grid in processing using the rect() function and I want to be able to move through the grid using the arrow keys and I'm not quite sure how to do it.
I want to start with one square coloured in and when I press an arrow key the square in that direction takes on that colour. For instance in a 10x10 grid, the cell in topmost left corner starts out blue, and when you press the down arrow key, the cell below turns blue (whilst the original cell loses its colour, becomes like every other cell in the grid)
The code I have so far is:
float cellSize = 30; //size of each square in cell
void setup ()
{
size (300, 300);
background (255);
}
void draw ()
{
stroke (0);
//loop to create grid
for (float posY = 0; posY < height; posY += cellSize)
{
for (float posX = 0; posX < width; posX += cellSize)
{
rect (posX, posY, cellSize, cellSize);
}
}
}
void keyPressed()
{
}
I'm assuming I'd have to use the keyPressed function with keyCode, can someone help out?
1