Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » keyboard event Switch Statement

This thread is locked; no one can reply to it. rss feed Print
keyboard event Switch Statement
AceBlkwell
Member #13,038
July 2011
avatar

All,

I've got a program that will react depending on which key is pressed (A~X). Currently I'm using a switch statement that reacts to 8 different keys. I'm wanting to expand that to 24. Do I really need 24 case statements or is there a better / compact way of accomplishing this?

Thanks

Matthew Leverton
Supreme Loser
January 1999
avatar

Using a switch statement with 24 case statements is a reasonable approach, especially if each key has a unique action associated with it. However, there might be more compact ways to achieve this, depending on your requirements.

One alternative is to use an array or a map to store the function pointers or objects representing the actions for each key. This solution works well if the actions can be easily represented as functions or objects and can generally make the code more manageable and potentially more extensible.

Here's an example using an array of function pointers:

#SelectExpand
1#include <allegro5/allegro.h> 2 3// Define a function type for your key actions 4typedef void (*KeyAction)(void); 5 6// Implement action functions for each key 7void actionA() { /* Do something for A key */ } 8void actionB() { /* Do something for B key */ } 9// ... (other actions) 10 11// Create an array of function pointers 12KeyAction keyActions[26] = {actionA, actionB, /* ... other actions */}; 13 14// In your event handling function 15ALLEGRO_EVENT event; 16// ... (get the event) 17if (event.type == ALLEGRO_EVENT_KEY_UP) { 18 int keyCode = event.keyboard.keycode; 19 if (keyCode >= ALLEGRO_KEY_A && keyCode <= ALLEGRO_KEY_Z) { 20 // Call the appropriate action function from the array 21 int index = keyCode - ALLEGRO_KEY_A; 22 keyActions[index](); 23 } 24}

If you find that there's a pattern to the actions or if they share some common logic, you might be able to simplify the code further by creating a single function that handles multiple keys based on their attributes. In this case, you can still use a switch statement or if-else conditions to determine which group of keys was pressed and perform the appropriate action.

It's also essential to consider code readability and maintainability with any approach. While a more compact solution may save some lines of code, it's important to ensure that the code is still easy to understand and modify later on.

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Matthew. I'll give it a look, thanks
Currently the cases are the same except it sets a true condition based on an array index. At the moment, I'm using numbers instead of letters. 1~8. So if I hit 2 then the case for ALLEGRO_KEY_2 will make Blocks[2].bFlipped = true. If I figure out a way to use a variable for the array index then I could use a function to activate the correct .bFlipped and increment the other variables.

Thanks.

Matthew Leverton
Supreme Loser
January 1999
avatar

In that case, you could create a single function to handle the key press event and set the bFlipped value in the Blocks array. If you're using numeric keys (1-8) and want to expand it to letters (A-X), you can easily adjust the index calculation accordingly.

Here's an example of how you can update the bFlipped value using a single function:

#SelectExpand
1#include <allegro5/allegro.h> 2 3// Assuming you have a Blocks structure and an array 4typedef struct { 5 bool bFlipped; 6 // ... (other members) 7} Block; 8 9Block Blocks[24]; // Array of blocks 10 11// Key handling function 12void handleKeyPress(int index) { 13 if (index >= 0 && index < 24) { 14 Blocks[index].bFlipped = true; 15 } 16} 17 18// In your event handling function 19ALLEGRO_EVENT event; 20// ... (get the event) 21if (event.type == ALLEGRO_EVENT_KEY_UP) { 22 int keyCode = event.keyboard.keycode; 23 24 int index = -1; 25 if (keyCode >= ALLEGRO_KEY_1 && keyCode <= ALLEGRO_KEY_8) { 26 index = keyCode - ALLEGRO_KEY_1; 27 } 28 else if (keyCode >= ALLEGRO_KEY_A && keyCode <= ALLEGRO_KEY_X) { 29 index = keyCode - ALLEGRO_KEY_A + 8; // Adding 8 to account for numeric keys 30 } 31 32 if (index != -1) { 33 handleKeyPress(index); 34 } 35}

This should simplify the event handling code and make it easier to add or modify behavior based on the array index.

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Matthew. I did something similar. First I'm sorry for the confusion but I am replacing the 1~8 with A~X. Not adding to. Right now I'm just playing with different ideas to get a process figured out. I started with 1~8 but expanded to 24 blocks and didn't want to figure out a way to do two digit numbers.

I went with ..

#SelectExpand
1 if(event.type == ALLEGRO_EVENT_KEY_DOWN) 2 { 3 iKey = event.keyboard.keycode-1; 4 5 if(event.keyboard.keycode >= ALLEGRO_KEY_A && event.keyboard.keycode <= ALLEGRO_KEY_X ){ 6 Blocks[iKey].bFlipped = true; 7 if(iChoiceCount == 0) iFirstChoice = iKey; 8 if(iChoiceCount == 1) iSecondChoice = iKey; 9 iChoiceCount++; 10 } // end if for block choice 11 else if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 12 bQuit = true;

I had to add the iKey because I use the event.keyboard as an index and my array starts with 0. Having event.keyboard.keycode -1 as an index seemed a little busy.
In case the goal seems odd, I'm creating a rough draft of a concentration / match type game.

Go to: