Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Mouse & Keyboard event interference

This thread is locked; no one can reply to it. rss feed Print
Mouse & Keyboard event interference
Asiron
Member #14,076
February 2012

Hello there,

I am a beginner in Allegro5 development, also in Game development.
My starting project is a simple SpaceInvaders themed game. I decided to let user control spaceship with either a keyboard or a mouse. The thing is that if I don't touch my mouse, keyboard control is working flawlessly. Same goes for mouse control, I chose to move my ship with left mouse button and fire gun with clicking right mouse button. Now here is where it still works, but if I choose to steer ship with left mouse button and fire gun with a space button on my keyboard, it starts to jump around. Even better any keystroke while moving my spaceship causes my ship to jump around to different positions, like if the stroke were processed by mouse and converted to x , y mouse positions. Don't know if that logic makes any sense from a programming point of view, but it kinda looks like it.

Here is my code:

#SelectExpand
1 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 2 { 3 switch(ev.keyboard.keycode) 4 { 5 case ALLEGRO_KEY_ESCAPE: 6 done = true; 7 break; 8 case ALLEGRO_KEY_UP: 9 keys[UP] = true; 10 break; 11 case ALLEGRO_KEY_DOWN: 12 keys[DOWN] = true; 13 break; 14 case ALLEGRO_KEY_RIGHT: 15 keys[RIGHT] = true; 16 break; 17 case ALLEGRO_KEY_LEFT: 18 keys[LEFT] = true; 19 break; 20 case ALLEGRO_KEY_SPACE: 21 keys[SPACE] = true; 22 FireBullet(bullets, NUM_BULLETS, ship); 23 break; 24 25 } 26 } 27 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 28 { 29 switch(ev.keyboard.keycode) 30 { 31 case ALLEGRO_KEY_UP: 32 keys[UP] = false; 33 break; 34 case ALLEGRO_KEY_DOWN: 35 keys[DOWN] = false; 36 break; 37 case ALLEGRO_KEY_RIGHT: 38 keys[RIGHT] = false; 39 break; 40 case ALLEGRO_KEY_LEFT: 41 keys[LEFT] = false; 42 break; 43 case ALLEGRO_KEY_SPACE: 44 keys[SPACE] = false; 45 break; 46 47 } 48 } 49 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 50 { 51 if (ev.mouse.button & 1) 52 { 53 mouse_buttons[LPM] = true; 54 al_set_mouse_xy(display, ship.x, ship.y); // causes my cursor to go back to ship when position of cursor has been changed while not holding left mouse button, doesn't allow "teleporting" 55 } 56 else if (ev.mouse.button & 2) 57 { 58 FireBullet(bullets, NUM_BULLETS, ship); 59 //mouse_buttons[PPM] = true; 60 } 61 } 62 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) 63 { 64 if (ev.mouse.button & 1) 65 {mouse_buttons[LPM] = false;} 66 else if (ev.mouse.button & 2) 67 {mouse_buttons[PPM] = false;} 68 } 69 else if(ev.type = ALLEGRO_EVENT_MOUSE_AXES) 70 { 71 if (mouse_buttons[LPM]) 72 MoveShipToXY(ship, ev.mouse.x, ev.mouse.y); 73 }

jmasterx
Member #11,410
October 2009

What if you gave the keyboard priority? If the left key or right key are true, ignore mouse move events.

Asiron
Member #14,076
February 2012

Thanks for fast respond.

The problem is however, that if I do that I won't be able to shoot with space button, and in future I won't be able to expand game, so that for example different types of missles use different keys to fire.

Thomas Fjellstrom
Member #476
June 2000
avatar

You can warp the pointer to the new location you've moved to with the keyboard.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: