bool ONScripter::keyPressEvent( SDL_KeyboardEvent *event )
{
    current_button_state.button = 0;
    current_button_state.down_flag = false;
    if ( automode_flag ){
        automode_flag = false;
        return false;
    }
    
    if ( event->type == SDL_KEYUP ){
        if ( variable_edit_mode ){
            variableEditMode( event );
            return false;
        }

        if ( edit_flag && event->keysym.sym == SDLK_z ){
            variable_edit_mode = EDIT_SELECT_MODE;
            variable_edit_sign = 1;
            variable_edit_num = 0;
            sprintf( wm_edit_string, "%s%s", EDIT_MODE_PREFIX, EDIT_SELECT_STRING );
            SDL_WM_SetCaption( wm_edit_string, wm_icon_string );
        }
    }
    
    if (event->type == SDL_KEYUP)
        skip_mode &= ~SKIP_NORMAL;
    
    if ( shift_pressed_status && event->keysym.sym == SDLK_q && current_mode == NORMAL_MODE ){
        endCommand();
    }

    if ( (trap_mode & TRAP_LEFT_CLICK) && 
         (event->keysym.sym == SDLK_RETURN ||
          event->keysym.sym == SDLK_KP_ENTER ||
          event->keysym.sym == SDLK_SPACE ) ){
        if (trapHandler()) return true;
    }
    else if ( (trap_mode & TRAP_RIGHT_CLICK) && 
              (event->keysym.sym == SDLK_ESCAPE) ){
        if (trapHandler()) return true;
    }
    
    if ( event_mode & WAIT_BUTTON_MODE &&
         (((event->type == SDL_KEYUP || btndown_flag) &&
           ((!getenter_flag && event->keysym.sym == SDLK_RETURN) ||
            (!getenter_flag && event->keysym.sym == SDLK_KP_ENTER))) ||
          ((spclclk_flag || !useescspc_flag) && event->keysym.sym == SDLK_SPACE)) ){
        if ( event->keysym.sym == SDLK_RETURN ||
             event->keysym.sym == SDLK_KP_ENTER ||
             (spclclk_flag && event->keysym.sym == SDLK_SPACE) ){
            current_button_state.button = current_over_button;
            if (current_over_button == 0)
                sprintf(current_button_state.str, "RETURN");
            else
                sprintf(current_button_state.str, "S%d", current_over_button);
            if ( event->type == SDL_KEYDOWN )
                current_button_state.down_flag = true;
        }
        else{
            current_button_state.button = 0;
            sprintf(current_button_state.str, "SPACE");
        }
        playClickVoice();
        stopAnimation( clickstr_state );

        return true;
    }

    if ( event->type == SDL_KEYDOWN ) return false;
    
    if ( ( event_mode & (WAIT_INPUT_MODE | WAIT_BUTTON_MODE) ) &&
         ( autoclick_time == 0 || (event_mode & WAIT_BUTTON_MODE)) ){
        if ( !useescspc_flag && event->keysym.sym == SDLK_ESCAPE){
            current_button_state.button  = -1;
            if (rmode_flag && event_mode & WAIT_TEXT_MODE){
                if (root_rmenu_link.next)
                    system_menu_mode = SYSTEM_MENU;
                else
                    system_menu_mode = SYSTEM_WINDOWERASE;
            }
        }
        else if ( useescspc_flag && event->keysym.sym == SDLK_ESCAPE ){
            current_button_state.button  = -10;
        }
        else if ( !spclclk_flag && useescspc_flag && event->keysym.sym == SDLK_SPACE ){
            current_button_state.button  = -11;
        }
        else if (((!getcursor_flag && event->keysym.sym == SDLK_LEFT) ||
                  event->keysym.sym == SDLK_h) &&
                 (event_mode & WAIT_TEXT_MODE ||
                  (usewheel_flag && !getcursor_flag && event_mode & WAIT_BUTTON_MODE) || 
                  system_menu_mode == SYSTEM_LOOKBACK)){
            current_button_state.button = -2;
            if (event_mode & WAIT_TEXT_MODE) system_menu_mode = SYSTEM_LOOKBACK;
        }
        else if ((!getcursor_flag && event->keysym.sym == SDLK_RIGHT ||
                  event->keysym.sym == SDLK_l) &&
                 (enable_wheeldown_advance_flag && event_mode & WAIT_TEXT_MODE ||
                  usewheel_flag && event_mode & WAIT_BUTTON_MODE || 
                  system_menu_mode == SYSTEM_LOOKBACK)){
            if (event_mode & WAIT_TEXT_MODE)
                current_button_state.button = 0;
            else
                current_button_state.button = -3;
        }
        else if ((!getcursor_flag && event->keysym.sym == SDLK_UP ||
                  event->keysym.sym == SDLK_k ||
                  event->keysym.sym == SDLK_p) &&
                 event_mode & WAIT_BUTTON_MODE){
            shiftCursorOnButton(1);
            return false;
        }
        else if ((!getcursor_flag && event->keysym.sym == SDLK_DOWN ||
                  event->keysym.sym == SDLK_j ||
                  event->keysym.sym == SDLK_n) &&
                 event_mode & WAIT_BUTTON_MODE){
            shiftCursorOnButton(-1);
            return false;
        }
        else if ( getpageup_flag && event->keysym.sym == SDLK_PAGEUP ){
            current_button_state.button  = -12;
            sprintf(current_button_state.str, "PAGEUP");
        }
        else if ( getpagedown_flag && event->keysym.sym == SDLK_PAGEDOWN ){
            current_button_state.button  = -13;
            sprintf(current_button_state.str, "PAGEDOWN");
        }
        else if ( getenter_flag && event->keysym.sym == SDLK_RETURN ||
                  getenter_flag && event->keysym.sym == SDLK_KP_ENTER ){
            current_button_state.button  = -19;
        }
        else if ( gettab_flag && event->keysym.sym == SDLK_TAB ){
            current_button_state.button  = -20;
        }
        else if ( getcursor_flag && event->keysym.sym == SDLK_UP ){
            current_button_state.button  = -40;
            sprintf(current_button_state.str, "UP");
        }
        else if ( getcursor_flag && event->keysym.sym == SDLK_RIGHT ){
            current_button_state.button  = -41;
            sprintf(current_button_state.str, "RIGHT");
        }
        else if ( getcursor_flag && event->keysym.sym == SDLK_DOWN ){
            current_button_state.button  = -42;
            sprintf(current_button_state.str, "DOWN");
        }
        else if ( getcursor_flag && event->keysym.sym == SDLK_LEFT ){
            current_button_state.button  = -43;
            sprintf(current_button_state.str, "LEFT");
        }
        else if ( getinsert_flag && event->keysym.sym == SDLK_INSERT ){
            current_button_state.button  = -50;
        }
        else if ( getzxc_flag && event->keysym.sym == SDLK_z ){
            current_button_state.button  = -51;
        }
        else if ( getzxc_flag && event->keysym.sym == SDLK_x ){
            current_button_state.button  = -52;
        }
        else if ( getzxc_flag && event->keysym.sym == SDLK_c ){
            current_button_state.button  = -53;
        }
        else if ( getfunction_flag && 
                  event->keysym.sym >= SDLK_F1 && event->keysym.sym <= SDLK_F12 ){
            current_button_state.button = -21-(event->keysym.sym - SDLK_F1);
            sprintf(current_button_state.str, "F%d", event->keysym.sym - SDLK_F1+1);
        }
        else if ( bexec_flag && 
                  event->keysym.sym >= SDLK_0 && event->keysym.sym <= SDLK_9 ){
            current_button_state.button = -1; // dummy
            sprintf(current_button_state.str, "%d", event->keysym.sym - SDLK_0);
        }
        else if ( bexec_flag && 
                  event->keysym.sym >= SDLK_a && event->keysym.sym <= SDLK_z ){
            current_button_state.button = -1; // dummy
            sprintf(current_button_state.str, "%c", 'A' + event->keysym.sym - SDLK_a);
        }
        else if ( bexec_flag && 
                  event->keysym.sym == SDLK_RSHIFT || event->keysym.sym == SDLK_LSHIFT ){
            current_button_state.button = -1; // dummy
            sprintf(current_button_state.str, "SHIFT");
        }
        
        if ( current_button_state.button != 0 ){
            stopAnimation( clickstr_state );

            return true;
        }
    }

    if ( event_mode & WAIT_INPUT_MODE &&
         ( autoclick_time == 0 || (event_mode & WAIT_BUTTON_MODE)) ){
        if (event->keysym.sym == SDLK_RETURN || 
            event->keysym.sym == SDLK_KP_ENTER ||
            event->keysym.sym == SDLK_SPACE ){
            if (!(event_mode & WAIT_TEXT_MODE))
                skip_mode |= SKIP_TO_EOL;
            playClickVoice();
            stopAnimation( clickstr_state );

            return true;
        }
    }
    
    if ( event_mode & WAIT_INPUT_MODE ){
        if (event->keysym.sym == SDLK_s && !automode_flag ){
            skip_mode |= SKIP_NORMAL;
            printf("toggle skip to true\n");
            stopAnimation( clickstr_state );

            return true;
        }
        else if (event->keysym.sym == SDLK_o){
            if (skip_mode & SKIP_TO_EOP)
                skip_mode &= ~SKIP_TO_EOP;
            else
                skip_mode |= SKIP_TO_EOP;
            printf("toggle draw one page flag to %s\n", (skip_mode & SKIP_TO_EOP?"true":"false") );
            if ( skip_mode & SKIP_TO_EOP ){
                stopAnimation( clickstr_state );

                return true;
            }
        }
        else if ( event->keysym.sym == SDLK_a && mode_ext_flag && !automode_flag ){
            automode_flag = true;
            skip_mode &= ~SKIP_NORMAL;
            printf("change to automode\n");
            stopAnimation( clickstr_state );

            return true;
        }
        else if ( event->keysym.sym == SDLK_0 ){
            if (++text_speed_no > 2) text_speed_no = 0;
            sentence_font.wait_time = -1;
        }
        else if ( event->keysym.sym == SDLK_1 ){
            text_speed_no = 0;
            sentence_font.wait_time = -1;
        }
        else if ( event->keysym.sym == SDLK_2 ){
            text_speed_no = 1;
            sentence_font.wait_time = -1;
        }
        else if ( event->keysym.sym == SDLK_3 ){
            text_speed_no = 2;
            sentence_font.wait_time = -1;
        }
    }

    if ( event_mode & ( WAIT_INPUT_MODE | WAIT_BUTTON_MODE ) ){
        if ( event->keysym.sym == SDLK_f ){
            if ( fullscreen_mode ) menu_windowCommand();
            else                   menu_fullCommand();
        }
    }

    return false;
}
bool ONScripter::mousePressEvent( SDL_MouseButtonEvent *event )
{
    if ( variable_edit_mode ) return false;
    
    if ( automode_flag ){
        automode_flag = false;
        return false;
    }

    if ( (event->button == SDL_BUTTON_RIGHT && trap_mode & TRAP_RIGHT_CLICK) ||
         (event->button == SDL_BUTTON_LEFT  && trap_mode & TRAP_LEFT_CLICK) ){
        if (trapHandler()) return true;
    }

    current_button_state.x = event->x;
    current_button_state.y = event->y;
    current_button_state.down_flag = false;
    skip_mode &= ~SKIP_NORMAL;

    if ( event->button == SDL_BUTTON_RIGHT &&
         event->type == SDL_MOUSEBUTTONUP &&
         ((rmode_flag && event_mode & WAIT_TEXT_MODE) ||
          (event_mode & (WAIT_BUTTON_MODE | WAIT_RCLICK_MODE))) ){
        current_button_state.button = -1;
        sprintf(current_button_state.str, "RCLICK");
        if (event_mode & WAIT_TEXT_MODE){
            if (root_rmenu_link.next)
                system_menu_mode = SYSTEM_MENU;
            else
                system_menu_mode = SYSTEM_WINDOWERASE;
        }
    }
    else if ( event->button == SDL_BUTTON_LEFT &&
              ( event->type == SDL_MOUSEBUTTONUP || btndown_flag ) ){
        current_button_state.button = current_over_button;
        if (current_over_button == 0)
            sprintf(current_button_state.str, "LCLICK");
        else
            sprintf(current_button_state.str, "S%d", current_over_button);
            
        if ( event->type == SDL_MOUSEBUTTONDOWN )
            current_button_state.down_flag = true;
    }
#if SDL_VERSION_ATLEAST(1, 2, 5)
    else if (event->button == SDL_BUTTON_WHEELUP &&
             (bexec_flag ||
              (event_mode & WAIT_TEXT_MODE) ||
              (usewheel_flag && event_mode & WAIT_BUTTON_MODE) || 
              system_menu_mode == SYSTEM_LOOKBACK)){
        current_button_state.button = -2;
        sprintf(current_button_state.str, "WHEELUP");
        if (event_mode & WAIT_TEXT_MODE) system_menu_mode = SYSTEM_LOOKBACK;
    }
    else if ( event->button == SDL_BUTTON_WHEELDOWN &&
              (bexec_flag ||
               (enable_wheeldown_advance_flag && event_mode & WAIT_TEXT_MODE) ||
               (usewheel_flag && event_mode & WAIT_BUTTON_MODE) || 
               system_menu_mode == SYSTEM_LOOKBACK ) ){
        if (event_mode & WAIT_TEXT_MODE)
            current_button_state.button = 0;
        else
            current_button_state.button = -3;
        sprintf(current_button_state.str, "WHEELDOWN");
    }
#endif
    else return false;

    if ( event_mode & (WAIT_INPUT_MODE | WAIT_BUTTON_MODE) ){
        if (!(event_mode & (WAIT_TEXT_MODE)))
            skip_mode |= SKIP_TO_EOL;
        playClickVoice();
        stopAnimation( clickstr_state );

        return true;
    }

    return false;
}
void PonscripterLabel::mousePressEvent(SDL_MouseButtonEvent* event)
{
    if (variable_edit_mode) return;

    if (automode_flag) {
        remaining_time = -1;
        setAutoMode(false);
        return;
    }

    if (event->button == SDL_BUTTON_RIGHT
        && trap_mode & TRAP_RIGHT_CLICK) {
        trapHandler();
        return;
    }
    else if (event->button == SDL_BUTTON_LEFT
             && trap_mode & TRAP_LEFT_CLICK) {
        trapHandler();
        return;
    }

    //Mouse didn't have a mouse-down event
    if(current_button_state.down_x == -1 && current_button_state.down_y == -1) {
        current_button_state.ignore_mouseup = true;
    }

    /* Use both = -1 to indicate we haven't received a mousedown yet */
    if(event->type == SDL_MOUSEBUTTONUP){
        current_button_state.down_x = -1;
        current_button_state.down_y = -1;
    }

    current_button_state.x = event->x;
    current_button_state.y = event->y;
    current_button_state.down_flag = false;
    setSkipMode(false);

    if (event->button == SDL_BUTTON_RIGHT
        && event->type == SDL_MOUSEBUTTONUP
        && !current_button_state.ignore_mouseup
        && ((rmode_flag && (event_mode & WAIT_TEXT_MODE))
            || (event_mode & WAIT_BUTTON_MODE))) {
        current_button_state.button  = -1;
        volatile_button_state.button = -1;
        if (event_mode & WAIT_TEXT_MODE) {
            if (!rmenu.empty())
                system_menu_mode = SYSTEM_MENU;
            else
                system_menu_mode = SYSTEM_WINDOWERASE;
        }
    }
    else if (event->button == SDL_BUTTON_LEFT
             && ((!current_button_state.ignore_mouseup && event->type == SDL_MOUSEBUTTONUP) || btndown_flag)) {
        current_button_state.button  = current_over_button;
        volatile_button_state.button = current_over_button;
//#ifdef SKIP_TO_WAIT
        if (event_mode & WAIT_SLEEP_MODE) skip_to_wait = 1;
//#endif

        if (event->type == SDL_MOUSEBUTTONDOWN) {
            current_button_state.down_flag = true;
        }
    } else {
      return;
    }

    if (event_mode & (WAIT_INPUT_MODE | WAIT_BUTTON_MODE)) {
        playClickVoice();
        stopAnimation(clickstr_state);
        advancePhase();
    }
}
void PonscripterLabel::keyPressEvent(SDL_KeyboardEvent* event)
{
    current_button_state.button = 0;
    current_button_state.down_flag = false;

    // This flag is set by anything before autmode that would like to not interrupt automode.
    // At present, this is volume mute and fullscreen. The commands don't simply return in case
    // the keypresses are handled below (e.g. telling the script the key 'a' was pressed)
    bool automode_ignore = false;

    if (event->type == SDL_KEYUP) {
        if (variable_edit_mode) {
            variableEditMode(event);
            return;
        }

        if (event->keysym.sym == SDLK_m) {
            volume_on_flag = !volume_on_flag;
            setVolumeMute(!volume_on_flag);
            printf("turned %s volume mute\n", !volume_on_flag?"on":"off");
            automode_ignore = true;
        }

        if (event->keysym.sym == SDLK_f) {
            if (fullscreen_mode) menu_windowCommand("menu_window");
            else menu_fullCommand("menu_full");
            return;
            automode_ignore = true;
        }

        if (edit_flag && event->keysym.sym == SDLK_z) {
            variable_edit_mode = EDIT_SELECT_MODE;
            variable_edit_sign = 1;
            variable_edit_num  = 0;
            wm_edit_string = EDIT_MODE_PREFIX EDIT_SELECT_STRING;
            SDL_SetWindowTitle(screen, wm_title_string);
        }
    }

    if (automode_flag && !automode_ignore) {
        remaining_time = -1;
        setAutoMode(false);
        return;
    }

    if (event->type == SDL_KEYUP
        && (event->keysym.sym == SDLK_RETURN
            || event->keysym.sym == SDLK_KP_ENTER
            || event->keysym.sym == SDLK_SPACE
            || event->keysym.sym == SDLK_s))
        setSkipMode(false);

    if (shift_pressed_status && event->keysym.sym == SDLK_q &&
	current_mode == NORMAL_MODE) {
        endCommand("end");
    }

    if ((trap_mode & TRAP_LEFT_CLICK)
        && (event->keysym.sym == SDLK_RETURN
            || event->keysym.sym == SDLK_KP_ENTER
            || event->keysym.sym == SDLK_SPACE)) {
        trapHandler();
        return;
    }
    else if ((trap_mode & TRAP_RIGHT_CLICK)
             && (event->keysym.sym == SDLK_ESCAPE)) {
        trapHandler();
        return;
    }

    const bool wait_button_mode = event_mode & WAIT_BUTTON_MODE;
    const bool key_or_btn = event->type == SDL_KEYUP || btndown_flag;
    const bool enter_key = !getenter_flag &&
	                   (event->keysym.sym == SDLK_RETURN ||
			    event->keysym.sym == SDLK_KP_ENTER);
    const bool space_lclick = spclclk_flag || !useescspc_flag;
    const bool space_key = event->keysym.sym == SDLK_SPACE;
    if (wait_button_mode && ((key_or_btn && enter_key) ||
			     (space_lclick && space_key))) {
	if (event->keysym.sym == SDLK_RETURN   ||
	    event->keysym.sym == SDLK_KP_ENTER ||
	    (spclclk_flag && event->keysym.sym == SDLK_SPACE))
	{
      current_button_state.button =
        volatile_button_state.button = current_over_button;
      if (event->type == SDL_KEYDOWN) {
          current_button_state.down_flag = true;
      }
  }
  else {
	    current_button_state.button =
	    volatile_button_state.button = 0;
	}
	playClickVoice();
	stopAnimation(clickstr_state);
	advancePhase();
	return;
    }

    if (event->type == SDL_KEYDOWN) return;

    if ((event_mode & (WAIT_INPUT_MODE | WAIT_BUTTON_MODE))
        && (autoclick_time == 0 || (event_mode & WAIT_BUTTON_MODE))) {
        if (!useescspc_flag && event->keysym.sym == SDLK_ESCAPE) {
            current_button_state.button = -1;
            if (rmode_flag && event_mode & WAIT_TEXT_MODE) {
                if (!rmenu.empty())
                    system_menu_mode = SYSTEM_MENU;
                else
                    system_menu_mode = SYSTEM_WINDOWERASE;
            }
        }
        else if (useescspc_flag && event->keysym.sym == SDLK_ESCAPE) {
            current_button_state.button = -10;
        }
        else if (!spclclk_flag && useescspc_flag && event->keysym.sym == SDLK_SPACE) {
            current_button_state.button = -11;
        }
        else if (((!getcursor_flag && event->keysym.sym == SDLK_LEFT) ||
                  event->keysym.sym == SDLK_h) &&
                 (event_mode & WAIT_TEXT_MODE ||
                  (usewheel_flag && !getcursor_flag &&
                   event_mode & WAIT_BUTTON_MODE) ||
                  system_menu_mode == SYSTEM_LOOKBACK))
	{
	    current_button_state.button  = -2;
            volatile_button_state.button = -2;
            if (event_mode & WAIT_TEXT_MODE) system_menu_mode = SYSTEM_LOOKBACK;
        }
        else if (((!getcursor_flag && event->keysym.sym == SDLK_RIGHT) ||
                  event->keysym.sym == SDLK_l) &&
                 ((enable_wheeldown_advance_flag &&
                   event_mode & WAIT_TEXT_MODE) ||
		  (usewheel_flag && event_mode & WAIT_BUTTON_MODE) ||
                  system_menu_mode == SYSTEM_LOOKBACK))
	{
	    if (event_mode & WAIT_TEXT_MODE) {
                current_button_state.button  = 0;
                volatile_button_state.button = 0;
            }
            else {
                current_button_state.button  = -3;
                volatile_button_state.button = -3;
            }
        }
	else if (((!getcursor_flag && event->keysym.sym == SDLK_UP) ||
                  event->keysym.sym == SDLK_k ||
                  event->keysym.sym == SDLK_p) &&
                 event_mode & WAIT_BUTTON_MODE){
            shiftCursorOnButton(1);
            return;
        }
        else if (((!getcursor_flag && event->keysym.sym == SDLK_DOWN) ||
                  event->keysym.sym == SDLK_j ||
                  event->keysym.sym == SDLK_n) &&
                 event_mode & WAIT_BUTTON_MODE){
            shiftCursorOnButton(-1);
            return;
        }
	else if (getpageup_flag && event->keysym.sym == SDLK_PAGEUP) {
            current_button_state.button = -12;
        }
        else if (getpagedown_flag && event->keysym.sym == SDLK_PAGEDOWN) {
            current_button_state.button = -13;
        }
        else if ((getenter_flag && event->keysym.sym == SDLK_RETURN)
                 || (getenter_flag && event->keysym.sym == SDLK_KP_ENTER)) {
            current_button_state.button = -19;
        }
        else if (gettab_flag && event->keysym.sym == SDLK_TAB) {
            current_button_state.button = -20;
        }
        else if (getcursor_flag && event->keysym.sym == SDLK_UP) {
            current_button_state.button = -40;
        }
        else if (getcursor_flag && event->keysym.sym == SDLK_RIGHT) {
            current_button_state.button = -41;
        }
        else if (getcursor_flag && event->keysym.sym == SDLK_DOWN) {
            current_button_state.button = -42;
        }
        else if (getcursor_flag && event->keysym.sym == SDLK_LEFT) {
            current_button_state.button = -43;
        }
        else if (getinsert_flag && event->keysym.sym == SDLK_INSERT) {
            current_button_state.button = -50;
        }
        else if (getzxc_flag && event->keysym.sym == SDLK_z) {
            current_button_state.button = -51;
        }
        else if (getzxc_flag && event->keysym.sym == SDLK_x) {
            current_button_state.button = -52;
        }
        else if (getzxc_flag && event->keysym.sym == SDLK_c) {
            current_button_state.button = -53;
        }
        else if (getfunction_flag) {
            if (event->keysym.sym == SDLK_F1)
                current_button_state.button = -21;
            else if (event->keysym.sym == SDLK_F2)
                current_button_state.button = -22;
            else if (event->keysym.sym == SDLK_F3)
                current_button_state.button = -23;
            else if (event->keysym.sym == SDLK_F4)
                current_button_state.button = -24;
            else if (event->keysym.sym == SDLK_F5)
                current_button_state.button = -25;
            else if (event->keysym.sym == SDLK_F6)
                current_button_state.button = -26;
            else if (event->keysym.sym == SDLK_F7)
                current_button_state.button = -27;
            else if (event->keysym.sym == SDLK_F8)
                current_button_state.button = -28;
            else if (event->keysym.sym == SDLK_F9)
                current_button_state.button = -29;
            else if (event->keysym.sym == SDLK_F10)
                current_button_state.button = -30;
            else if (event->keysym.sym == SDLK_F11)
                current_button_state.button = -31;
            else if (event->keysym.sym == SDLK_F12)
                current_button_state.button = -32;
        }

        if (current_button_state.button != 0) {
            volatile_button_state.button = current_button_state.button;
            stopAnimation(clickstr_state);
            advancePhase();
            return;
        }
    }

    if (event_mode & WAIT_INPUT_MODE && !key_pressed_flag
        && (autoclick_time == 0 || (event_mode & WAIT_BUTTON_MODE))) {
        if (event->keysym.sym == SDLK_RETURN
            || event->keysym.sym == SDLK_KP_ENTER
            || event->keysym.sym == SDLK_SPACE) {
            key_pressed_flag = true;
            playClickVoice();
            stopAnimation(clickstr_state);
            advancePhase();
        }
    }

    if (event_mode & (WAIT_INPUT_MODE | WAIT_TEXTBTN_MODE)
        && !key_pressed_flag) {
        if (event->keysym.sym == SDLK_s && !automode_flag) {
            setSkipMode(true);
            //printf("toggle skip to true\n");
            key_pressed_flag = true;
            stopAnimation(clickstr_state);
            advancePhase();
        }
        else if (event->keysym.sym == SDLK_o) {
            draw_one_page_flag = !draw_one_page_flag;
            //printf("toggle draw one page flag to %s\n", (draw_one_page_flag ? "true" : "false"));
            if (draw_one_page_flag) {
                stopAnimation(clickstr_state);
                advancePhase();
            }
        }
        else if (event->keysym.sym == SDLK_a && mode_ext_flag &&
                 !automode_flag)
        {
            setAutoMode(true);
            //printf("change to automode\n");
            key_pressed_flag = true;
            stopAnimation(clickstr_state);
            advancePhase();
        }
        else if (event->keysym.sym == SDLK_0) {
            if (++text_speed_no > 2) text_speed_no = 0;

            sentence_font.wait_time = -1;
        }
        else if (event->keysym.sym == SDLK_1) {
            text_speed_no = 0;
            sentence_font.wait_time = -1;
        }
        else if (event->keysym.sym == SDLK_2) {
            text_speed_no = 1;
            sentence_font.wait_time = -1;
        }
        else if (event->keysym.sym == SDLK_3) {
            text_speed_no = 2;
            sentence_font.wait_time = -1;
        }
    }

    if (event_mode & WAIT_SLEEP_MODE) {
      if (event->keysym.sym == SDLK_RETURN ||
          event->keysym.sym == SDLK_KP_ENTER ||
          event->keysym.sym == SDLK_SPACE) {
        skip_to_wait = 1;
      }
    }
}
bool ONScripter::mousePressEvent( SDL_MouseButtonEvent *event )
{
    if ( variable_edit_mode ) return false;
    
    if ( automode_flag ){
        automode_flag = false;
        return false;
    }

    if ( (event->button == SDL_BUTTON_RIGHT && trap_mode & TRAP_RIGHT_CLICK) ||
         (event->button == SDL_BUTTON_LEFT  && trap_mode & TRAP_LEFT_CLICK) ){
        if (trapHandler()) return true;
    }

    current_button_state.x = event->x * screen_width / screen_device_width;
    current_button_state.y = event->y * screen_width / screen_device_width;
    current_button_state.down_flag = false;
    skip_mode &= ~SKIP_NORMAL;

    if ( event->button == SDL_BUTTON_RIGHT &&
         event->type == SDL_MOUSEBUTTONUP &&
         ((rmode_flag && event_mode & WAIT_TEXT_MODE) ||
          (event_mode & (WAIT_BUTTON_MODE | WAIT_RCLICK_MODE))) ){
        current_button_state.button = -1;
        sprintf(current_button_state.str, "RCLICK");
        if (event_mode & WAIT_TEXT_MODE){
            if (root_rmenu_link.next)
                system_menu_mode = SYSTEM_MENU;
            else
                system_menu_mode = SYSTEM_WINDOWERASE;
        }
    }
    else if ( event->button == SDL_BUTTON_LEFT &&
              ( event->type == SDL_MOUSEBUTTONUP || btndown_flag ) ){
        current_button_state.button = current_over_button;
        if (current_over_button == -1){
            if (!bexec_flag) current_button_state.button = 0;
            sprintf(current_button_state.str, "LCLICK");
        }
        else{
            sprintf(current_button_state.str, "S%d", current_over_button);
            if (bexec_flag && current_button_link){
                ButtonLink *cbl = current_button_link;
                if ( current_button_link->exbtn_ctl[2] ){
                    SDL_Rect check_src_rect = cbl->image_rect;
                    SDL_Rect check_dst_rect = {0, 0, 0, 0};
                    decodeExbtnControl( cbl->exbtn_ctl[2], &check_src_rect, &check_dst_rect );
                }
                else{
                    sprite_info[ cbl->sprite_no ].visible = true;
                    sprite_info[ cbl->sprite_no ].setCell(2);
                    dirty_rect.add( cbl->image_rect );
                }
                flush( refreshMode() );
            }
        }
            
        if ( event->type == SDL_MOUSEBUTTONDOWN )
            current_button_state.down_flag = true;
    }
#if SDL_VERSION_ATLEAST(1, 2, 5)
    else if (event->button == SDL_BUTTON_WHEELUP &&
             (bexec_flag ||
              (event_mode & WAIT_TEXT_MODE) ||
              (usewheel_flag && event_mode & WAIT_BUTTON_MODE) || 
              system_menu_mode == SYSTEM_LOOKBACK)){
        current_button_state.button = -2;
        sprintf(current_button_state.str, "WHEELUP");
        if (event_mode & WAIT_TEXT_MODE) system_menu_mode = SYSTEM_LOOKBACK;
    }
    else if ( event->button == SDL_BUTTON_WHEELDOWN &&
              (bexec_flag ||
               (enable_wheeldown_advance_flag && event_mode & WAIT_TEXT_MODE) ||
               (usewheel_flag && event_mode & WAIT_BUTTON_MODE) || 
               system_menu_mode == SYSTEM_LOOKBACK ) ){
        if (event_mode & WAIT_TEXT_MODE)
            current_button_state.button = 0;
        else
            current_button_state.button = -3;
        sprintf(current_button_state.str, "WHEELDOWN");
    }
#endif
    else if ( getmclick_flag && event->button == SDL_BUTTON_MIDDLE ){
        current_button_state.button = -70;
        sprintf(current_button_state.str, "MCLICK");
    }
    else return false;

    if ( event_mode & (WAIT_INPUT_MODE | WAIT_BUTTON_MODE) ){
        if (!(event_mode & (WAIT_TEXT_MODE)))
            skip_mode |= SKIP_TO_EOL;
        playClickVoice();
        stopAnimation( clickstr_state );

        return true;
    }

    return false;
}