void GameChat::step(GameState* gs) { std::string& msg = typed_message.message; if (show_chat) fade_out = 1.0f; else if (fade_out > 0.0f) fade_out -= fade_out_rate; if (repeat_steps_left > 0) repeat_steps_left--; else if (current_key != SDLK_FIRST) { /*Handle keys being held down*/ if (is_typeable_keycode(current_key)) { msg += keycode_to_char(current_key, current_mod); repeat_steps_left = NEXT_REPEAT_STEP_AMNT; } else if (current_key == SDLK_BACKSPACE) { if (msg.empty()) { reset_typed_message(); is_typing = false; } else { msg.resize(msg.size() - 1); } repeat_steps_left = NEXT_BACKSPACE_STEP_AMNT; } } }
int backend_dispatch_event(void) { int key = 0; XEvent event; XEvent *ev = &event; while (XPending(display)) { XNextEvent(display, ev); switch (event.type) { case ConfigureNotify: frontend_resize(((XConfigureEvent*)ev)->width, ((XConfigureEvent*)ev)->height, shiftkey_pressed); break; case Expose: break; case ClientMessage: if ((Atom)event.xclient.data.l[0] == delete_window_atom) key = 'q'; break; case KeyPress: case KeyRelease: key = keycode_to_char((XKeyEvent*)ev); break; default: //printf("got event: %d\n", ev->response_type & ~0x80); break; } } return key; }
/* process_character: [fdwatch thread] * This is where most of the work gets done. CH is a byte read in * from the keyboard's fd. This function finds the Allegro equivalent * of that key code, figures out which ASCII character that key * generates (if any), and then calls the handle_key_press() and * handle_key_release() functions with that information. It also does * some things with modifier keys. */ static void process_character(unsigned char ch) { /* read kernel's keycode and convert to Allegro's keycode */ int keycode = ch & 0x7f; bool press = !(ch & 0x80); int mycode = kernel_to_mycode[keycode]; /* if the key was something weird we don't understand, skip it */ if (mycode == 0) return; /* process modifiers */ if (mycode >= ALLEGRO_KEY_MODIFIERS) { int flag = modifier_table[mycode - ALLEGRO_KEY_MODIFIERS]; if (press) { if (flag & KB_MODIFIERS) the_keyboard.modifiers |= flag; else if ((flag & KB_LED_FLAGS) && the_keyboard.key_led_flag) the_keyboard.modifiers ^= flag; } else { /* XXX: if the user presses LCTRL, then RCTRL, then releases * LCTRL, the ALLEGRO_KEYMOD_CTRL modifier should still be on. */ if (flag & KB_MODIFIERS) the_keyboard.modifiers &= ~flag; } } /* call the handlers */ if (press) { int ascii = keycode_to_char(keycode); /* switch VT if the user requested so */ if (ascii < 0) { int console = -ascii; int last_console; ioctl(the_keyboard.fd, VT_OPENQRY, &last_console); if (console < last_console) if (ioctl(the_keyboard.fd, VT_ACTIVATE, console) == 0) return; } handle_key_press(mycode, ascii); } else { handle_key_release(mycode); } /* three-finger salute for killing the program */ if ((the_keyboard.three_finger_flag) && ((mycode == ALLEGRO_KEY_DELETE) || (mycode == ALLEGRO_KEY_END)) && (the_keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) && (the_keyboard.modifiers & ALLEGRO_KEYMOD_ALT)) { kill(main_pid, SIGTERM); } }
/*Returns whether has handled event*/ void TextField::step() { if (!_has_repeat_cooldown() && _current_key != SDLK_FIRST) { /*Handle keys being held down*/ if (is_typeable_keycode(_current_key)) { if (_text.size() < _max_length) { _text += keycode_to_char(_current_key, _current_mod); } _reset_repeat_cooldown(NEXT_REPEAT_MS); } else if (_current_key == SDLK_BACKSPACE) { _handle_backspace(); _reset_repeat_cooldown(NEXT_BACKSPACE_MS); } } }
bool TextField::handle_event(SDL_Event* event) { SDLKey keycode = event->key.keysym.sym; SDLMod keymod = event->key.keysym.mod; _current_mod = keymod; switch (event->type) { case SDL_KEYUP: { /*Since the key-up isnt truly an action, we respond but pretend we didn't handle it*/ if (_current_key == keycode) { _current_key = SDLK_FIRST; } break; } case SDL_KEYDOWN: { if (is_typeable_keycode(keycode)) { if (_text.size() < _max_length) { _text += keycode_to_char(keycode, keymod); } if (_current_key != keycode) { _current_key = keycode; _reset_repeat_cooldown(INITIAL_REPEAT_MS); } return true; } if (keycode == SDLK_BACKSPACE) { _handle_backspace(); if (_current_key != keycode) { _current_key = keycode; _reset_repeat_cooldown(INITIAL_REPEAT_MS); } return true; } if (keycode == SDLK_DELETE) { _text.clear(); return true; } break; } } return false; }
/*Returns whether has handled event completely or not*/ bool GameChat::handle_event(GameState* gs, SDL_Event *event) { int view_w = gs->view().width, view_h = gs->view().height; int chat_w = view_w, chat_h = 100; int chat_x = 0, chat_y = 0; //h - chat_h - TILE_SIZE; SDLKey keycode = event->key.keysym.sym; SDLMod keymod = event->key.keysym.mod; current_mod = keymod; switch (event->type) { case SDL_MOUSEBUTTONDOWN: { // if (show_chat && event->button.button == SDL_BUTTON_LEFT // && gs->mouse_x() < chat_w && gs->mouse_y() < chat_h) { // toggle_chat(gs); // return true; // } break; } case SDL_KEYUP: { if (current_key == keycode) current_key = SDLK_FIRST; /*Let GameState handle this as well*/ break; } case SDL_KEYDOWN: { if (keycode == SDLK_RETURN) { toggle_chat(gs); return true; } if (is_typing) { std::string& msg = typed_message.message; if (is_typeable_keycode(keycode)) { msg += keycode_to_char(keycode, keymod); if (current_key != keycode) { current_key = keycode; repeat_steps_left = INITIAL_REPEAT_STEP_AMNT; } return true; } if (keycode == SDLK_BACKSPACE) { if (msg.empty()) { reset_typed_message(); is_typing = false; } else { msg.resize(msg.size() - 1); } if (current_key != keycode) { current_key = keycode; repeat_steps_left = INITIAL_REPEAT_STEP_AMNT; } return true; } if (keycode == SDLK_LCTRL || keycode == SDLK_RCTRL) { is_typing = false; return true; } if (keycode == SDLK_DELETE) { reset_typed_message(); is_typing = false; return true; } } break; } } return false; }