-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyEventHandler.java
157 lines (117 loc) · 4.08 KB
/
KeyEventHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Font;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Clipboard;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class KeyEventHandler
{
static String buffer = "";
static String japanese = "";
public static void printText(JLabel label, char c)
{
buffer += Character.toString(c);
//copies text to clipboard when enter key is pressed and resets japanese string
if(c == KeyEvent.VK_ENTER)
{
StringSelection stringSelection = new StringSelection(japanese);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
japanese = "";
buffer = "";
}
//TODO: handle backspace
if(c == KeyEvent.VK_BACK_SPACE)
{
System.out.println("BACKSPACE DETECTED");
}
if(JapaneseKeyCodes.toJapanese.get(buffer) != null)
{
japanese += JapaneseKeyCodes.toJapanese.get(buffer);
buffer = "";
}
label.setText(japanese);
}
//MESSY PROGRAMMING -- <!Make Backup First!> Clean this up when it is working
public static void main(String[] args)
{
JFrame frame = new JFrame("English to Hiragana");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int w = 800;
int h = 800;
frame.setSize(w,h);
JLabel toJapanese = new JLabel("");
toJapanese.setBounds(400,400,w,h);
toJapanese.setFont(new Font(toJapanese.getName(), Font.PLAIN, 36));
frame.add(toJapanese);
//initialize hashmap dictionary
JapaneseKeyCodes.populateMap();
Container contentPane = frame.getContentPane();
KeyListener listener = new KeyListener()
{
@Override
public void keyPressed(KeyEvent event)
{
printEventInfo(" Key Pressed ", event);
}
@Override
public void keyReleased(KeyEvent event)
{
printEventInfo(" Key Released ", event);
}
//TODO: add to a string buffer the key that has been typed -- this will be used later
//LOGIC: If the next character is a vowel and the current character in the buffer is a consonant -- conver to consonant and vowel hiragana vers.
// If the next character and the current character in the buffer are vowels -- covert both to vowel hiragana
// remove characters that have been converted
// if no next character -- convert individual
//COLLISIONS: "n" hiragana
//Solutions: Polling and creating a time variable and "feeling" how quickly it should convert
//GOALS:
// Current -> Create a conversion system to hiragana;
// Data Structures involved -- Dictionary/Hashmap
@Override
public void keyTyped(KeyEvent event)
{
printText(toJapanese, event.getKeyChar());
printEventInfo(" Key Typed ", event);
}
private void printEventInfo(String str, KeyEvent e)
{
System.out.println(str);
int keyCode = e.getKeyCode();
System.out.println(" Code: " + KeyEvent.getKeyText(keyCode));
System.out.println(" Char: " + e.getKeyChar());
int mods = e.getModifiersEx();
System.out.println(" Mods: " + KeyEvent.getModifiersExText(mods));
System.out.println(" Location: " + keyboardLocation(e.getKeyLocation()));
System.out.println(" Action? " + e.isActionKey());
}
private String keyboardLocation(int keybrd)
{
switch(keybrd)
{
case KeyEvent.KEY_LOCATION_RIGHT:
return "Right";
case KeyEvent.KEY_LOCATION_LEFT:
return "Left";
case KeyEvent.KEY_LOCATION_NUMPAD:
return "NumPad";
case KeyEvent.KEY_LOCATION_STANDARD:
return "Standard";
case KeyEvent.KEY_LOCATION_UNKNOWN:
default:
return "Unknown";
}
}
};
JTextField textField = new JTextField();
textField.addKeyListener(listener);
contentPane.add(textField, BorderLayout.NORTH);
frame.setVisible(true);
}
}