shVector<const char *> * shNCursesInterface::getKeysForCommand (Command cmd) { shVector<const char *> *keys = new shVector<const char *> (); for (int i = 0; i < KEY_MAX; ++i) { if (cmd == mKey2Cmd[i]) keys->add (keyCodeToString (i)); } return keys; }
const char * shNCursesInterface::getKeyForCommand (Command cmd) { /* Find key corresponding to command. */ int i; for (i = 0; i < KEY_MAX and cmd != mKey2Cmd[i]; ++i) ; if (i == KEY_MAX) /* Unassigned? This is bad. */ return NULL; /* Safe, will not lead to crash. */ return keyCodeToString (i); }
void InputControlSystem::addKeyBinding(Control* control, SDL_Keycode key, Control::ControlChangingDirection direction) { ICS_LOG("\tAdding KeyBinder [key=" + keyCodeToString(key) + ", direction=" + ToString<int>(direction) + "]"); ControlKeyBinderItem controlKeyBinderItem; controlKeyBinderItem.control = control; controlKeyBinderItem.direction = direction; mControlsKeyBinderMap[ key ] = controlKeyBinderItem; }
/* * main: Start listening to keyboard and mouse events, and copying them to the serial port */ int main(void) { // The terminal window. Note that mouse tracking only works if the terminal is running inside a GUI (e.g. LXDE) WINDOW * terminalWindow; // The key code as an integer int keyCode; // The previous mouse coordinates int lastXValue = 0; int lastYValue = 0; // The current mouse coordinates int xValue = 0; int yValue = 0; // The character used is calculated from the difference of values int xChar = 0; int yChar = 0; // Change this to make the mouse move faster or slower int trackingSpeed = 10; // This is part of the mouse tracking protocol, which currently uses characters int characterOffset = 64; // Mouse mask mmask_t old; // The character to send to the serial port char* thisCharacter; // Open the serial port int serialPort = open(SERIAL1, O_RDWR | O_NOCTTY | O_NDELAY); // The serial port file reference that we write characters to FILE *serialPortFile; // The log file for saving a local keystroke log FILE *logFile; // Open the serial port file for writing serialPortFile = fopen(SERIAL1, "w"); // Open the log file for writing logFile = fopen("log.txt", "w"); // Initialize ncurses if ( (terminalWindow = initscr()) == NULL ) { fprintf(stderr, "Error initializing ncurses.\n"); exit(EXIT_FAILURE); } // end if ncurses can't be initalised // Turn off key echoing noecho(); // Enable the keypad for non-char keys keypad(terminalWindow, TRUE); cbreak(); // Capture mouse events too mousemask (ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, &old); // Print a prompt and refresh() the screen mvaddstr(5, 10, "Press a key ('q' to quit)..."); mvprintw(7, 10, "You pressed: "); refresh(); // Capture new inputs from the keyboard or mouse until user presses 'q' while ( (keyCode = getch()) != 'q' ) { // If it was a mouse event if (keyCode == KEY_MOUSE) { // The mouse event MEVENT event; //assert (getmouse(&event) == OK); //mvprintw (0,0,"Mouse Event!\n"); // If the mouse event can be read if( getmouse( &event ) == OK ) { // Left mouse click if (event.bstate & BUTTON1_CLICKED) { // Show visual feedback about the left mouse click mvprintw(7, 10, "You clicked the mouse"); // Send 'b' (button) to the serial port fprintf(serialPortFile, "%s", "b"); fflush(serialPortFile); // Send 'l' (left click) to the serial port fprintf(serialPortFile, "%s", "l"); fflush(serialPortFile); } else { // Right mouse click if (event.bstate & BUTTON3_CLICKED) { // Show visual feedback about the right mouse click mvprintw(7, 10, "You right clicked the mouse"); // Send 'b' (button) to the serial port fprintf(serialPortFile, "%s", "b"); fflush(serialPortFile); // Send 'r' (right click) to the serial port fprintf(serialPortFile, "%s", "r"); fflush(serialPortFile); } else { // Read the mouse location and scale by the tracking speed xValue = event.x * trackingSpeed; yValue = event.y * trackingSpeed; // Calculate the difference from the last value xChar = lastXValue - xValue + characterOffset; yChar = lastYValue - yValue + characterOffset; // Show visual feedback about the mouse movement mvprintw(7, 10, "You moved the mouse %d %d", xChar, yChar); // Send 'x' (horizontal) to the serial port fprintf(serialPortFile, "%s", "x"); fflush(serialPortFile); // Send the x coordinate character to the serial port fprintf(serialPortFile, "%c", xChar); fflush(serialPortFile); // Send 'y' (vertical) to the serial port fprintf(serialPortFile, "%s", "y"); fflush(serialPortFile); // Send the y coordinate character to the serial port fprintf(serialPortFile, "%c", yChar); fflush(serialPortFile); // Send '?' (terminator) to the serial port fprintf(serialPortFile, "%s", "?"); fflush(serialPortFile); // Update the last value variable lastXValue = xValue; lastYValue = yValue; } // end if right click or movement } // end if left click } // end if mouse event } else { /* Delete the old response line, and print a new one */ deleteln(); thisCharacter = keyCodeToString(keyCode); //write(serialPort, thisCharacter, 2); fprintf(serialPortFile, "%s", "k"); fflush(serialPortFile); fprintf(serialPortFile, "%s", thisCharacter); fflush(serialPortFile); fprintf(logFile, "%s", thisCharacter); fflush(logFile); mvprintw(7, 10, "You pressed: 0x%x (%s)", keyCode, thisCharacter); refresh(); } // end if mouse or keyboard } // end while key pressed isn't 'q' // Clean up the GUI delwin(terminalWindow); endwin(); refresh(); // Close the serial port close (serialPort); // Quit return EXIT_SUCCESS; } // end function main