Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Multiple User Key Input

This thread is locked; no one can reply to it. rss feed Print
[A5] Multiple User Key Input
sa1k01
Member #13,707
November 2011

Hi @ all,

i have a question regarding the user key input. Say i have a options dialog with some options like resolution and several key settings for the player.

i know, that i can store a single user input with:

al_ustr_append_chr(ALLEGRO_USTR, event.keyboard.unichar);

but how does this work for multiple input fields? Is there a tutorial for this kind of problem? ^^

greetings
sa1k01

Edgar Reynaldo
Member #8,592
May 2007
avatar

sa1k01
Member #13,707
November 2011

thx for the reply.

mh ok. then i should just wait for an mouse click event and save the x and y position of the label, right?

i will give it a try and let you know if this works.

kind regards
sa1k01

Edgar Reynaldo
Member #8,592
May 2007
avatar

If you're using a struct/class to store your input field object, then just use a pointer to the active object, or store a flag in the class :

#SelectExpand
1class InputField { 2private : 3 int x,y,w,h; 4 string text; 5 int caretx; 6 bool hasfocus; 7public : 8 bool CheckInput(ALLEGRO_EVENT ev) { 9 tookfocus = false; 10 if (hasfocus) { 11 // check for unicode key char events here 12 } 13 else { 14 // something else 15 if (ev.type == ALLEGRO_EVENT_MOUSE_DOWN) { 16 tookfocus = Click(ev.mouse.x , ev.mouse.y); 17 } 18 } 19 return tookfocus; 20 } 21 22 void SetFocus(bool focus) {hasfocus = focus;} 23}

#SelectExpand
1 2InputField in1,in2,in3; 3InputField* infield[3] = {&in1 , &in2 , &in3}; 4 5InputField* infocus = 0; 6 7//... 8 ALLEGRO_EVENT ev; 9 al_wait_for_event(event_queue , &ev); 10 for (int i = 0 ; i < 3 ; ++i) { 11 if (infield[i]->CheckInput(ev)) { 12 infocus = infield[i]; 13 for (int j = 0 ; j < 3 ; ++j) { 14 infield[i]->SetFocus(infield[i] == infocus); 15 } 16 } 17 }

Go to: