Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Enter charaters/numbers from Keypad

This thread is locked; no one can reply to it. rss feed Print
Enter charaters/numbers from Keypad
Kokxxx12
Member #17,515
March 2020

Hi, I create kind od board game in Allegro and I want input numbers and dispplay these numbers on the screen. I try using sscanf and sprintf but still numbers dont display on the screen.

int width = 0;
int length = 40;
char buff[256];
char buff1[256];
char a;

if(key[KEY_RIGHT])
{
if(width<=340)
{
clear_to_color(buffer,makecol(255,255,0));
width+=40;
blit(pole1,buffer,0,0,width,length,40,40);
clear_to_color(pole1,makecol(128,0,0));
sscanf(buff,"%d",&a);

textout_ex(buffer,font,buff,width+20,length+20, makecol(255, 0, 128),-1);

rest(100);

MikiZX
Member #17,092
June 2019

Personally I do not see how you enter the numbers in the 'buff'.

Possibly 'fgets' or 'getline' are missing in your source code.

If I was programming this without the standard C functions I would do it like this:

#SelectExpand
1// initialization 2// initialization 3// initialization 4 5 ALLEGRO_KEYBOARD_STATE kbdstate; 6 char buff[256]; 7 int buff_pos; 8 int a; // better use INT though CHAR might work as well - i am not sure 9 10 if (!al_install_keyboard()) 11 { 12 abort_example("Error installing keyboard.\n"); 13 return 1; 14 } 15 buff_pos=0; 16 17 18 19// in your program's main loop 20// (this would execute many times to receive multiple numbers) 21// in your program's main loop 22// in your program's main loop 23 24 al_get_keyboard_state(&kbdstate); 25 26 if (al_key_down(&kbdstate, ALLEGRO_KEY_PAD_0)) 27 { 28 buff[buff_pos]=0; 29 buff_pos++; 30 } 31 32 if (al_key_down(&kbdstate, ALLEGRO_KEY_PAD_1)) 33 { 34 buff[buff_pos]=1; 35 buff_pos++; 36 } 37 38 if (al_key_down(&kbdstate, ALLEGRO_KEY_PAD_2)) 39 { 40 buff[buff_pos]=2; 41 buff_pos++; 42 } 43 44 .... 45 46 if (al_key_down(&kbdstate, ALLEGRO_KEY_PAD_9)) 47 { 48 buff[buff_pos]=9; 49 buff_pos++; 50 } 51 52 if (buff_pos>255) buff_pos=0; 53 54 printf("%s \n", buff); // print on screen 55 56 sscanf(buff,"%d",&a); // convert text buffer to integer 'a'

Kokxxx12
Member #17,515
March 2020

I don't understand this code at all. Can you describe please what are you doing in this code?

DuncanShine
Member #17,479
March 2020

I just avoided all the buffer stuff and converted to const char* using .c_str() then outputted that using al_draw_text.

Kokxxx12
Member #17,515
March 2020

What doing this program? I creating sudoku game and I want to input numbers into the grid from keypad

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

MikiZX
Member #17,092
June 2019

Yeah, possibly your num pad lock is off.

if you wish a complete example program this should help (once you run this program you should see two windows - one with text showing the key you pressed and one with graphics that shows different color depending on the key pressed):

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3 4const float FPS = 60; 5 6ALLEGRO_DISPLAY *display = NULL; 7ALLEGRO_EVENT_QUEUE *event_queue = NULL; 8ALLEGRO_TIMER *timer1 = NULL; 9 10bool redraw = true; 11int input_number = 0; 12 13// isolating cleanup code in a single function because we use this code many times 14// doing this will keep the rest of the source code easier to read 15void close_and_exit() 16{ 17 if (timer1) al_destroy_timer(timer1); 18 if (display) al_destroy_display(display); 19 if (event_queue) al_destroy_event_queue(event_queue); 20 al_uninstall_system(); 21} 22 23// the program execution starts here with 'main' function 24int main(int argc, char **argv){ 25 26 // here we initialize different parts of Allegro 27 if(!al_init()) { 28 fprintf(stderr, "failed to initialize allegro!\n"); 29 return -1; 30 } 31 32 display = al_create_display(640, 480); 33 if(!display) { 34 fprintf(stderr, "failed to create display!\n"); 35 close_and_exit(); 36 } 37 38 if (!al_install_keyboard()) 39 { 40 fprintf(stderr, "Error installing keyboard.\n"); 41 close_and_exit(); 42 } 43 44 event_queue = al_create_event_queue(); 45 if(!event_queue) { 46 fprintf(stderr, "failed to create event_queue!\n"); 47 close_and_exit(); 48 } 49 50 timer1 = al_create_timer(1.0 / FPS); 51 if(!timer1) { 52 fprintf(stderr, "failed to create timer!\n"); 53 close_and_exit(); 54 } 55 56 al_register_event_source(event_queue, al_get_keyboard_event_source()); 57 58 al_register_event_source(event_queue, al_get_display_event_source(display)); 59 60 al_register_event_source(event_queue, al_get_timer_event_source(timer1)); 61 62 al_start_timer(timer1); 63 64 65 // this is the main loop that will receive keypresses, do game logic processing and draw our game on the screen 66 while(1) 67 { 68 ALLEGRO_EVENT ev; 69 al_wait_for_event(event_queue, &ev); 70 71 if(ev.type == ALLEGRO_EVENT_TIMER) { 72 redraw = true; 73 } 74 75 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { 76 if (ev.keyboard.keycode>=ALLEGRO_KEY_PAD_1 && ev.keyboard.keycode<=ALLEGRO_KEY_PAD_9) 77 // calculate input number from input keycode 78 // since the ALLEGRO keycodes for numeric pad are sequential 79 // (i.e. ALLEGRO_KEY_PAD_0 is value of 36, ALLEGRO_KEY_PAD_1 is value of 37, 80 // ALLEGRO_KEY_PAD_2 is value of 38, ... until ALLEGRO_KEY_PAD_9 81 // we can take the input keycode and subtract value of ALLEGRO_KEY_PAD_0 to get the 82 // inputed number ... i.e. ALLEGRO_KEY_PAD_1-ALLEGRO_KEY_PAD_0=1 because 37-26=1 83 input_number=ev.keyboard.keycode-ALLEGRO_KEY_PAD_0; 84 else 85 input_number=0; 86 87 if (ev.keyboard.keycode==ALLEGRO_KEY_ESCAPE) 88 close_and_exit(); 89 } 90 91 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 92 break; 93 } 94 95 if(redraw && al_is_event_queue_empty(event_queue)) 96 { 97 redraw = false; 98 // convert inputed number to colors (0=black, 1=blue, 2=red, ...) 99 int r,g,b; 100 r=((input_number&2)/2) * 127; 101 g=((input_number&4)/4) * 127; 102 b=(input_number&1) * 127; 103 al_clear_to_color(al_map_rgb(r,g,b)); // clear graphics screen 104 105 system("@cls||clear"); // clear the text console window 106 printf("%d\n",input_number); // print out the number entered 107 108 al_flip_display(); 109 } 110 } 111 close_and_exit(); 112}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

MikiZX
Member #17,092
June 2019

Thanks Edgar, fixed now.

@Kokxxx12, I hope this can help in some way - if you still have difficulties understanding how it works then please let us know which part you do understand and we will explain the rest.

LennyLen
Member #5,313
December 2004
avatar

It might be time to point out that the OP is using A4, not A5.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I need fucking glasses.

Kokxxx12, with Allegro 4 you can use readkey and shift the value 8 bits to get the keycode, then convert it to a number or digit.

MikiZX
Member #17,092
June 2019

oops.. glasses for me too then. Sorry Kokxxx12, now I understand why the code was not understandable.
I don't think I will be able to help with A4 though.
@LennyLen, thank you for pointing this out.

Go to: