Skip to content

Commit

Permalink
Add configurable key order & minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RoanH committed Feb 6, 2017
1 parent 1171854 commit a288122
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 41 deletions.
14 changes: 10 additions & 4 deletions KeysPerSecond/src/me/roan/kps/KeyPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public final class KeyPanel extends JPanel {
private static final Font font1 = new Font("Dialog", Font.BOLD, 24);
/**
* Font 2 used to display the value of this panel
* this font is changed when the value that has to
* be displayed goes out of the panel bounds
*/
protected Font font2 = new Font("Dialog", Font.PLAIN, 18);
private Font font2 = new Font("Dialog", Font.PLAIN, 18);
/**
* Font 2 but smaller
*/
private Font font2small = new Font("Dialog", Font.PLAIN, 14);

/**
* Constructs a new KeyPanel
Expand Down Expand Up @@ -67,7 +69,11 @@ public void paintComponent(Graphics g1) {
g.setFont(BasePanel.font1);
}
g.drawString(key.name, (this.getWidth() - g.getFontMetrics().stringWidth(key.name)) / 2, 30);
g.setFont(font2);
if(key.count >= 1000){
g.setFont(font2small);
}else{
g.setFont(font2);
}
String str = String.valueOf(key.count);
g.drawString(str, (this.getWidth() - g.getFontMetrics().stringWidth(str)) / 2, 55);
}
Expand Down
103 changes: 66 additions & 37 deletions KeysPerSecond/src/me/roan/kps/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
Expand Down Expand Up @@ -41,9 +40,11 @@
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
Expand Down Expand Up @@ -122,11 +123,6 @@ public class Main {
* used during the initial setup
*/
private static NativeKeyEvent lastevent;
/**
* String containing all the tracked keys
* only used during the initial setup
*/
private static String addedkeys = "";
/**
* Key configuration data, can be serialised
*/
Expand Down Expand Up @@ -173,11 +169,6 @@ public static void main(String[] args) {
//Get a configuration for the keys
boolean[] fields = configure();

//Register the keys to monitor
for(KeyInformation i : keyinfo){
keys.put(i.keycode, new Key(i.name));
}

//Build GUI
try {
buildGUI(fields[0], fields[1], fields[2], fields[3]);
Expand Down Expand Up @@ -389,18 +380,56 @@ private static final boolean[] configure(){
GraphPanel.MAX = (int)backlog.getValue();
});
addkey.addActionListener((e)->{
if(JOptionPane.showConfirmDialog(null, "Press a key and press OK to add it\nCurrently added keys: " + addedkeys, "Keys per second", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION){
JPanel keyform = new JPanel(new BorderLayout());
JPanel text = new JPanel(new GridLayout(2, 1));
text.add(new JLabel("Press a key and press 'Add Key' to add it"), BorderLayout.PAGE_START);
text.add(new JLabel("Currently added keys (you can edit the position):"), BorderLayout.PAGE_START);
keyform.add(text, BorderLayout.PAGE_START);
JTable keys = new JTable();
keys.setBorder(BorderFactory.createLineBorder(Color.BLACK));
keys.setModel(new DefaultTableModel(){
/**
* Serial ID
*/
private static final long serialVersionUID = -5510962859479828507L;

@Override
public int getRowCount() {
return keyinfo.size() + 1;
}

@Override
public int getColumnCount() {
return 2;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return rowIndex == 0 ? (columnIndex == 0 ? "Position" : "Key") : (columnIndex == 0 ? keyinfo.get(rowIndex - 1).index : keyinfo.get(rowIndex - 1).name);
}

@Override
public boolean isCellEditable(int row, int col){
return col == 0 && row != 0;
}

@Override
public void setValueAt(Object value, int row, int col){
try{
keyinfo.get(row - 1).index = Integer.parseInt((String)value);
}catch(NumberFormatException | NullPointerException e){
JOptionPane.showMessageDialog(null, "Entered position not a (whole) number!", "Keys per second", JOptionPane.ERROR_MESSAGE);
}
}
});
keyform.add(keys, BorderLayout.CENTER);
if(JOptionPane.showOptionDialog(null, keyform, "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[]{"Add Key", "Back"}, 0) == 0){
if(lastevent == null){
JOptionPane.showMessageDialog(null, "No key pressed!", "Keys per second", JOptionPane.ERROR_MESSAGE);
return;
}
KeyInformation info = new KeyInformation(NativeKeyEvent.getKeyText(lastevent.getKeyCode()), lastevent.getKeyCode());
if(JOptionPane.showConfirmDialog(null, "Add the " + info.name + " key?", "Keys per second", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
if(addedkeys.isEmpty()){
addedkeys += info.name;
}else{
addedkeys += ", " + info.name;
}
keyinfo.add(info);
save.setEnabled(true);
}
Expand All @@ -412,7 +441,7 @@ private static final boolean[] configure(){
if(chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION){
return;
};
File saveloc = new File(chooser.getSelectedFile().getAbsolutePath() + ".kpsconf");
File saveloc = new File(chooser.getSelectedFile().getAbsolutePath().endsWith(".kpsconf") ? chooser.getSelectedFile().getAbsolutePath() : (chooser.getSelectedFile().getAbsolutePath() + ".kpsconf"));
if(!saveloc.exists() || (saveloc.exists() && JOptionPane.showConfirmDialog(null, "File already exists, overwrite?", "Keys per second", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)){
try {
ObjectOutputStream objout = new ObjectOutputStream(new FileOutputStream(saveloc));
Expand Down Expand Up @@ -442,7 +471,6 @@ private static final boolean[] configure(){
try {
ObjectInputStream objin = new ObjectInputStream(new FileInputStream(saveloc));
keyinfo = (List<KeyInformation>) objin.readObject();
addedkeys = "";
cmax.setSelected(objin.readBoolean());
ccur.setSelected(objin.readBoolean());
cavg.setSelected(objin.readBoolean());
Expand All @@ -454,14 +482,12 @@ private static final boolean[] configure(){
GraphPanel.MAX = objin.readInt();
timeframe = objin.readInt();
objin.close();
for(KeyInformation i : keyinfo){
if(addedkeys.isEmpty()){
addedkeys += i.name;
}else{
addedkeys += ", " + i.name;
save.setEnabled(true);
for(KeyInformation info : keyinfo){
if(info.index > KeyInformation.autoIndex){
KeyInformation.autoIndex = info.index + 1;
}
}
save.setEnabled(true);
JOptionPane.showMessageDialog(null, "Config succesfully loaded", "Keys per second", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Failed to load the config!", "Keys per second", JOptionPane.ERROR_MESSAGE);
Expand Down Expand Up @@ -515,7 +541,10 @@ private static final void buildGUI(boolean max, boolean avg, boolean cur, boolea
unpressed = ImageIO.read(ClassLoader.getSystemResource("key.png"));
JFrame frame = new JFrame("Keys per second");
content.setBackground(Color.BLACK);
for (Key k : keys.values()) {
keyinfo.sort((KeyInformation left, KeyInformation right) -> (left.index > right.index ? 1 : -1));
Key k;
for(KeyInformation i : keyinfo){
keys.put(i.keycode, k = new Key(i.name));
content.add(k.getPanel());
}
int extra = 0;
Expand Down Expand Up @@ -599,7 +628,7 @@ public void mouseMoved(MouseEvent e) {
//================== NESTED CLASSES ===============================================================
//=================================================================================================

/**ff
/**
* This class is used to keep track
* of how many times a specific key
* is pressed
Expand All @@ -614,11 +643,6 @@ protected static final class Key {
* The total number of times this key has been pressed
*/
protected int count = 0;
/**
* The {@link KeyPanel} responsible for
* displaying information about the tracked key
*/
private KeyPanel kp;
/**
* The key in string form<br>
* For example: X
Expand All @@ -642,7 +666,7 @@ private Key(String name) {
* @return A new KeyPanel
*/
private KeyPanel getPanel() {
return kp = new KeyPanel(this);
return new KeyPanel(this);
}

/**
Expand All @@ -654,9 +678,6 @@ private void keyPressed() {
if (!down) {
count++;
down = true;
if (count >= 1000) {
kp.font2 = new Font("Dialog", Font.PLAIN, 14);
}
tmp++;
}
}
Expand All @@ -682,7 +703,7 @@ private static final class KeyInformation implements Serializable{
/**
* Serial ID
*/
private static final long serialVersionUID = -3752409253121094171L;
private static final long serialVersionUID = -8669938978334898443L;
/**
* The name of this key
* @see Key#name
Expand All @@ -693,6 +714,14 @@ private static final class KeyInformation implements Serializable{
* This code represents the key
*/
private int keycode;
/**
* Index of the key
*/
private int index = autoIndex++;
/**
* Auto-increment for #index
*/
private static transient int autoIndex = 0;

/**
* Constructs a new KeyInformation
Expand Down

0 comments on commit a288122

Please sign in to comment.