/** * Method declaration * * @param k */ public void keyTyped(KeyEvent k) { if (k.getKeyChar() == '\n' && k.isControlDown()) { k.consume(); execute(); } }
public void keyPressed(KeyEvent e) { // System.out.println("keyPressed"); if (focussed) { switch (e.getKeyCode()) { case KeyEvent.VK_W: // case KeyEvent.VK_W uo: pacman.moveUp(); break; case KeyEvent.VK_Z: // case KeyEvent.VK_Z down: pacman.moveDown(); break; case KeyEvent.VK_S: // case KeyEvent.VK_S right: pacman.moveUp(); break; case KeyEvent.VK_A: // case KeyEvent.VK_A left: pacman.moveUp(); break; case KeyEvent.VK_UP: // case KeyEvent.VK_KP_UP: pacman.moveUp(); break; case KeyEvent.VK_DOWN: // case KeyEvent.VK_KP_DOWN: pacman.moveDown(); break; case KeyEvent.VK_RIGHT: // case KeyEvent.VK_KP_RIGHT: pacman.moveRight(); break; case KeyEvent.VK_LEFT: // case KeyEvent.VK_KP_LEFT: pacman.moveLeft(); break; } } // end if focussed }
@Override public void keyPressed(KeyEvent e) { int option; // The option the user selected. User buyer; // User object for buyer User seller; // User object for seller Item selected_item; // Item object for selected item if (help) // if in help mode { return; } switch (e.getKeyCode()) // handle event associated with key { case KeyEvent.VK_F1: // if f1 was pressed switchStore(0); break; case KeyEvent.VK_F2: // if f2 was pressed switchStore(1); break; case KeyEvent.VK_F3: // if f3 was pressed switchStore(2); break; case KeyEvent.VK_F4: // if f4 was pressed switchStore(3); break; case KeyEvent.VK_D: // if d or right was pressed case KeyEvent.VK_RIGHT: if (currentPage != totalPages - 1) // if we are not on the last page { currentPage++; itemSelected = -1; } break; case KeyEvent.VK_A: // if a or left was pressed case KeyEvent.VK_LEFT: if (currentPage != 0) // if we are not on the first page { currentPage--; itemSelected = -1; } break; case KeyEvent.VK_W: // if w or up was pressed case KeyEvent.VK_UP: if (itemSelected == -1) // if no items currently selected { itemSelected = 0; } else // if an item was selected { itemSelected--; if (itemSelected < 0) // loop if at beginning of list { itemSelected = ITEMSPERPAGE - 1; } } break; case KeyEvent.VK_S: // if s or down was pressed case KeyEvent.VK_DOWN: itemSelected++; if (itemSelected >= ITEMSPERPAGE) // if at end of list, loop around { itemSelected = 0; } break; case KeyEvent.VK_ENTER: // if enter was pressed if (itemSelected == -1) // if no item was selected { break; } selected_item = itemsArray[itemSelected]; option = JOptionPane.showConfirmDialog( this, ((!mode) ? "Buy " : "Sell ") + "for " + "$" + selected_item.getPrice() + "?"); if (option == 0 && mode) // if they choose to buy { buyer = usersArray[0]; switch (store) // change message based on value of store. { case 0: seller = usersArray[2]; break; case 1: seller = usersArray[1]; break; case 2: seller = usersArray[3]; break; case 3: seller = usersArray[4]; break; default: seller = usersArray[1]; } buyItemFromUser(buyer, seller, selected_item); } else if (option == 0 && mode) // if they choose to sell { seller = usersArray[0]; switch (store) // change message based on value of store. { case 0: buyer = usersArray[2]; break; case 1: buyer = usersArray[1]; break; case 2: buyer = usersArray[3]; break; case 3: buyer = usersArray[4]; break; default: buyer = usersArray[1]; } buyItemFromUser(buyer, seller, selected_item); } itemSelected = -1; break; default: return; } repaint(); }