public void init() { try { GlobalScreen.registerNativeHook(); } catch (NativeHookException ex) { ex.printStackTrace(); } createInstaces(); addListeners(); }
@Override public void init() throws Exception { super.init(); try { GlobalScreen.registerNativeHook(); } catch (NativeHookException e) { e.printStackTrace(); System.exit(1); } // Add listeners GlobalScreen.addNativeMouseListener(this); GlobalScreen.addNativeMouseMotionListener(this); GlobalScreen.addNativeMouseWheelListener(this); // Disable parent logger and set the desired level. logger.setUseParentHandlers(false); logger.setLevel(Level.ALL); }
public StreamKB(final JFrame frame) { super(new GridLayout(6, 1)); // Adds keyboard listener to entire screen even when program isn't selected try { GlobalScreen.registerNativeHook(); } catch (NativeHookException ex) { System.err.println("There was a problem registering the native hook."); System.err.println(ex.getMessage()); ex.printStackTrace(); System.exit(1); } GlobalScreen.getInstance().addNativeKeyListener(this); // Loads saved colors from file, otherwise sets initial Colors try { FileInputStream loadColors = new FileInputStream("StreamKB.Settings"); ObjectInputStream load = new ObjectInputStream(loadColors); bgC = (Color) load.readObject(); btC = (Color) load.readObject(); hiC = (Color) load.readObject(); txtC = (Color) load.readObject(); frame.setLocation((Point) load.readObject()); mySize = (String) load.readObject(); load.close(); } catch (Exception e) { bgC = new Color(200, 200, 200); btC = new Color(238, 238, 238); hiC = new Color(0, 255, 0); txtC = new Color(0, 0, 0); mySize = "Big"; } // Declare panels for each row on keyboard final JPanel fKeys = new JPanel(); fKeys.setLayout(new FlowLayout(FlowLayout.TRAILING, 1, 1)); final JPanel numKeys = new JPanel(); numKeys.setLayout(new FlowLayout(FlowLayout.TRAILING, 1, 1)); final JPanel tabLine = new JPanel(); tabLine.setLayout(new FlowLayout(FlowLayout.TRAILING, 1, 1)); final JPanel capsLine = new JPanel(); capsLine.setLayout(new FlowLayout(FlowLayout.TRAILING, 1, 1)); final JPanel shiftLine = new JPanel(); shiftLine.setLayout(new FlowLayout(FlowLayout.TRAILING, 1, 1)); final JPanel ctrlLine = new JPanel(); ctrlLine.setLayout(new FlowLayout(FlowLayout.TRAILING, 1, 1)); final JPanel upDown = new JPanel(); upDown.setLayout(new BorderLayout(0, 0)); // set background color for each panel fKeys.setBackground(bgC); numKeys.setBackground(bgC); tabLine.setBackground(bgC); capsLine.setBackground(bgC); shiftLine.setBackground(bgC); ctrlLine.setBackground(bgC); upDown.setBackground(bgC); setBackground(bgC); // frame.setBackground(bgC); // List of key values for each row Integer[] fKeyList = { 27, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 44, 19, 45, 46 }; Integer[] numKeyList = {192, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 189, 187, 8}; Integer[] tabLineList = {9, 81, 87, 69, 82, 84, 89, 85, 73, 79, 80, 219, 221, 220}; Integer[] capsLineList = {20, 65, 83, 68, 70, 71, 72, 74, 75, 76, 186, 222, 13}; Integer[] shiftLineList = {160, 90, 88, 67, 86, 66, 78, 77, 188, 190, 191, 161}; Integer[] ctrlLineList = {162, 91, 164, 32, 165, 93, 163}; // Put all buttons into KB HashMap so they can be accessed later by key value makeButtons(fKeyList); makeButtons(numKeyList); makeButtons(tabLineList); makeButtons(capsLineList); makeButtons(shiftLineList); makeButtons(ctrlLineList); // Put all keys into keyboard addButtons(fKeys, fKeyList); addButtons(numKeys, numKeyList); addButtons(tabLine, tabLineList); addButtons(capsLine, capsLineList); addButtons(shiftLine, shiftLineList); addButtons(ctrlLine, ctrlLineList); // manually adding arrow keys for formatting KB.put((Integer) 37, new KBButton(37)); KB.put((Integer) 38, new KBButton(38)); KB.put((Integer) 39, new KBButton(39)); KB.put((Integer) 40, new KBButton(40)); ctrlLine.add(KB.get((Integer) 37)); upDown.add(KB.get((Integer) 38), BorderLayout.NORTH); upDown.add(KB.get((Integer) 40), BorderLayout.SOUTH); ctrlLine.add(upDown); ctrlLine.add(KB.get((Integer) 39)); // adds all button rows to main panel add(fKeys); add(numKeys); add(tabLine); add(capsLine); add(shiftLine); add(ctrlLine); // Allow window to be dragged // Get start point addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { clickPos = e.getPoint(); getComponentAt(clickPos); } }); // If drag, move addMouseMotionListener( new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { // get location of Window int thisX = frame.getLocation().x; int thisY = frame.getLocation().y; // Determine how much the mouse moved since the initial click int xMoved = (thisX + e.getX()) - (thisX + clickPos.x); int yMoved = (thisY + e.getY()) - (thisY + clickPos.y); // Move window to this position int X = thisX + xMoved; int Y = thisY + yMoved; frame.setLocation(X, Y); } }); // add right click menu addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e)) { JPopupMenu menu = new JPopupMenu(); JMenuItem optionsMenuItem = new JMenuItem("Color Options"); optionsMenuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // Make frame for pop-up window JPanel OptionsPane = new JPanel(); OptionsPane.setLayout(new GridLayout(4, 0)); // Make panels for each color change option JPanel bgColor = new JPanel(); JPanel btColor = new JPanel(); JPanel hiColor = new JPanel(); JPanel txtColor = new JPanel(); // generate options for each pane and add them to pop-up window OptionsPane.add(colorOption(bgColor, bgC, "Background")); OptionsPane.add(colorOption(btColor, btC, "Button")); OptionsPane.add(colorOption(hiColor, hiC, "Highlight")); OptionsPane.add(colorOption(txtColor, txtC, "Text")); // Pop-up! int result = JOptionPane.showConfirmDialog( null, OptionsPane, "Select Desired Colors", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); // Apply color changes if OK button hit. if (result == JOptionPane.OK_OPTION) { bgC = bgColor.getBackground(); btC = btColor.getBackground(); hiC = hiColor.getBackground(); txtC = txtColor.getBackground(); fKeys.setBackground(bgC); numKeys.setBackground(bgC); tabLine.setBackground(bgC); capsLine.setBackground(bgC); shiftLine.setBackground(bgC); ctrlLine.setBackground(bgC); upDown.setBackground(bgC); setBackground(bgC); frame.setBackground(bgC); for (KBButton b : KB.values()) { b.setForeground(txtC); b.setBorder(BorderFactory.createLineBorder(txtC)); b.setBackground(btC); b.repaint(); } repaint(); // save changes to settings try { FileOutputStream saveColors = new FileOutputStream("StreamKB.settings", false); ObjectOutputStream save = new ObjectOutputStream(saveColors); save.writeObject(bgC); save.writeObject(btC); save.writeObject(hiC); save.writeObject(txtC); save.writeObject(frame.getLocation()); save.writeObject(mySize); save.close(); } catch (Exception e1) { e1.printStackTrace(); } } } }); JMenuItem Close = new JMenuItem("Exit"); Close.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // saves window variables before closing try { FileOutputStream saveColors = new FileOutputStream("StreamKB.settings", false); ObjectOutputStream save = new ObjectOutputStream(saveColors); save.writeObject(bgC); save.writeObject(btC); save.writeObject(hiC); save.writeObject(txtC); save.writeObject(frame.getLocation()); save.writeObject(mySize); save.close(); } catch (Exception e1) { e1.printStackTrace(); } System.exit(0); } }); JMenuItem sizeMenuItem = new JMenuItem(); if (mySize.equals("Big")) { sizeMenuItem.setText("Shrink Keyboard"); } else { sizeMenuItem.setText("Enlarge Keyboard"); } sizeMenuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (mySize.equals("Big")) { mySize = "Small"; for (KBButton b : KB.values()) { b.shrink(); } frame.pack(); } else { mySize = "Big"; for (KBButton b : KB.values()) { b.enlarge(); } frame.pack(); } } }); menu.add(optionsMenuItem); menu.add(sizeMenuItem); menu.add(Close); menu.show(e.getComponent(), e.getX(), e.getY()); } } }); // Change color of all of the buttons and add copy mouse listeners from main window to each // button for (KBButton b : KB.values()) { b.setForeground(txtC); b.setBorder(BorderFactory.createLineBorder(txtC)); b.setBackground(btC); for (MouseListener m : this.getMouseListeners()) { b.addMouseListener(m); } for (MouseMotionListener m : this.getMouseMotionListeners()) { b.addMouseMotionListener(m); } if (!mySize.equals("Big")) { b.shrink(); } } }