01: /*
02: * KeyboardMapper.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.settings;
13:
14: import java.awt.BorderLayout;
15: import java.awt.Color;
16: import java.awt.event.KeyEvent;
17: import java.awt.event.KeyListener;
18: import java.awt.event.WindowAdapter;
19: import java.awt.event.WindowEvent;
20:
21: import javax.swing.JFrame;
22: import javax.swing.JPanel;
23: import javax.swing.JTextField;
24: import javax.swing.KeyStroke;
25:
26: /**
27: *
28: * @author support@sql-workbench.net
29: */
30: public class KeyboardMapper extends JPanel implements KeyListener {
31: private JTextField display;
32: private KeyStroke newkey;
33:
34: public KeyboardMapper() {
35: this .display = new JTextField(20);
36: this .display.addKeyListener(this );
37: this .display.setEditable(false);
38: this .display.setDisabledTextColor(Color.BLACK);
39: this .display.setBackground(Color.WHITE);
40: this .add(display);
41: }
42:
43: public void grabFocus() {
44: this .display.grabFocus();
45: this .display.requestFocusInWindow();
46: }
47:
48: public void keyPressed(KeyEvent e) {
49: }
50:
51: public void keyReleased(KeyEvent e) {
52: int modifier = e.getModifiers();
53: int code = e.getKeyCode();
54:
55: // only allow function keys without modifier!
56: if (modifier == 0) {
57: if (code < KeyEvent.VK_F1 || code > KeyEvent.VK_F12)
58: return;
59: }
60:
61: // keyReleased is also called when the Ctrl or Shift keys are release
62: // in that case the keycode is 0 --> ignore it
63: if (code >= 32) {
64: String key = "";
65: if (modifier > 0)
66: key = KeyEvent.getKeyModifiersText(modifier) + "-";
67: key = key + KeyEvent.getKeyText(code);
68: this .newkey = KeyStroke.getKeyStroke(code, modifier);
69: this .display.setText(key);
70: }
71: }
72:
73: public KeyStroke getKeyStroke() {
74: return this .newkey;
75: }
76:
77: public void keyTyped(KeyEvent e) {
78: }
79:
80: }
|