Remapping the Keyboard at a low level?

If I were doing this, I would write the Java App without worrying about the key bindings. Assume that when a component gets a keyevent for #7 its #7, don't worry about whether the 7 or 1 was really typed. The app shouldn't care about how keys are mapped on the keyboard.

This should let you start developing the app immediately As far as overriding key bindings, this seems like where you want to look: download.oracle.com/javase/7/docs/api/ja... It sounds like you can write your own KeyEventDispatcher to handle all the key mapping logic and it prevents mapping logic from messing up the rest of the logic in your application.

If I were doing this, I would write the Java App without worrying about the key bindings. Assume that when a component gets a keyevent for #7 its #7, don't worry about whether the 7 or 1 was really typed. The app shouldn't care about how keys are mapped on the keyboard.

This should let you start developing the app immediately. As far as overriding key bindings, this seems like where you want to look: download.oracle.com/javase/7/docs/api/ja... It sounds like you can write your own KeyEventDispatcher to handle all the key mapping logic and it prevents mapping logic from messing up the rest of the logic in your application.

Meverett: No, that won't work. For example, there are many dialog tabs. When the user types "7" while focused on a text box, "1", not "7" will appear.

Using Key Bindings, I see "71" when I do that. Tried KeyEventDispatcher, but can't make that work either so far. See earlier post on that.

– Steve Cohen Jun 15 at 17:45 Are you consuming the first KeyEvent before broadcasting the desired mapped key? It sounds like the KeyEventDispatcher is what you want, just need to spend sometime figuring out exactly what its doing. – meverett Jun 15 at 18:01 I agree that KeyEventDispatcher sounds like what I want, but it doesn't seem to work.

The simple technique outlined here: exampledepot. Com/egs/java. Awt/DispatchKey.

Html#comment-51807 doesn't seem to get us there (the untranslated keystroke appears in the text box, not the translated one) and it's not really well documented. Looking for a solid example of this. – Steve Cohen Jun 15 at 18:20 @Steve Cohen, The code from that example worked fine for me.

I use JDK6_07 on XP. Post the actual SSCCE (sscce. Org) that you used to text the code so other can test it as well to see if this is a version/platform issue.

– camickr Jun 15 at 19:56 I guess I should try to run that code myself and see what the difference is to what I was doing. I THOUGHT I was copying his method, and it was pretty simple, but somehow it didn't work. – Steve Cohen Jun 15 at 20:24.

This is hacky, and I'll admit I haven't used it myself, but you could subclass KeyEvent and override the fields as needed. So yourSubclass. VK_NUMPAD1 is the integer value of KeyEvent.

VK_NUMPAD7, yourSubclass. VK_NUMPAD2 is the integer value of KeyEvent. VK_NUMPAD8, etcetera.

Then use your subclass everywhere KeyEvent is normally used.

I also agree that the KeyEventDispatcher should work. But if this a version/platform issue, then maybe you can use a custom Event Queue. See Global Event Dispatching.

My stack overfloweth! The answer was here: download.oracle.com/javase/6/docs/api/ja... which says: For key pressed and key released events, the getKeyCode method returns the event's keyCode. For key typed events, the getKeyCode method always returns VK_UNDEFINED.

My original attempt thought it could get a keyCode on KEY_TYPED. It could not, and it was the KEY_TYPED event that was clobbering the mapping done in KEY_PRESSED. Here is a working implementation: import static java.awt.event.KeyEvent.

*; import java.awt. BorderLayout; import java.awt. Color; import java.awt.

Dimension; import java.awt. KeyEventDispatcher; import java.awt. KeyboardFocusManager; import java.awt.event.

KeyEvent; import java.util. HashMap; import java.util. Map; import javax.swing.

JFrame; import javax.swing. JPanel; import javax.swing. JTextArea; import javax.swing.

JTextField; import javax.swing. SwingConstants; public class KeyboardDispatcherDemo extends JFrame { /** * This class shows how to map numeric keypad keys. * It performs two conversions: * 1.

Lower-to-uppercase * 2. If numeric keypad 7-9 is typed, 1-3 appears and vice versa. * * This is modified from the code at * http://www.exampledepot.com/egs/java.awt/DispatchKey.html#comment-51807 * which demoes the lower-to-upper conversion.

* * It doesn't yet handle modified numeric keypad keys. * */ public KeyboardDispatcherDemo() { KeyboardFocusManager. GetCurrentKeyboardFocusManager().

AddKeyEventDispatcher( new KeyEventDispatcher() { private char lastMappedKey; private final Map keyMap = new HashMap() { { put(VK_NUMPAD1, '7'); put(VK_NUMPAD2, '8'); put(VK_NUMPAD3, '9'); put(VK_NUMPAD7, '1'); put(VK_NUMPAD8, '2'); put(VK_NUMPAD9, '3'); }}; public boolean dispatchKeyEvent(KeyEvent e) { System.out. Println(String. Format("INPUT: %s", e.toString())); boolean dispatch = false; switch (e.getID()) { case KeyEvent.

KEY_PRESSED: dispatch = dispatchKeyPressed(e); break; case KeyEvent. KEY_TYPED: dispatch = dispatchKeyTyped(e); break; case KeyEvent. KEY_RELEASED: dispatch = dispatchKeyReleased(e); break; default: throw new IllegalArgumentException(); } System.out.

Println(String. Format("OUTPUT: %s", e.toString())); System.out.println(); return dispatch; } private boolean dispatchKeyPressed(KeyEvent e) { char k = e.getKeyChar(); if (k! = CHAR_UNDEFINED) { if (Character.

IsLetter(k)) { e. SetKeyChar(Character. ToUpperCase(e.getKeyChar())); } else if (e.getModifiers() == 0){ Character mapping = keyMap.

Get(e.getKeyCode()); if (mapping! = null) { e. SetKeyChar(mapping); } } // save the last mapping so that KEY_TYPED can use it.

// note we don't do this for modifier keys. This. LastMappedKey = e.getKeyChar(); } return false; } // KEY_TYPED events don't have keyCodes so we rely on the // lastMappedKey that was saved on KeyPressed.

Private boolean dispatchKeyTyped(KeyEvent e) { char k = e.getKeyChar(); if (k! = CHAR_UNDEFINED) { e. SetKeyChar(lastMappedKey); } return false; } private boolean dispatchKeyReleased(KeyEvent e) { char k = e.getKeyChar(); if (k!

= CHAR_UNDEFINED) { e. SetKeyChar(lastMappedKey); this. LastMappedKey=CHAR_UNDEFINED; } return false; } }); setTitle("KeyboardDispatcherDemo"); JPanel panel = new JPanel(); panel.

SetBackground(new Color(204, 153, 255)); panel. SetLayout(new BorderLayout()); getContentPane(). Add(panel, BorderLayout.

CENTER); JTextArea staticText = new JTextArea(); staticText. SetText("This demonstrates how to map numeric keypad keys. It uppercases all letters and converts Numeric Keypad 1-3 to 7-9 and vice versa.

Try it. "); staticText. SetLineWrap(true); staticText.

SetWrapStyleWord(true); panel. Add(staticText, BorderLayout. NORTH); staticText.

SetFocusable(false); JTextField textField = new JTextField(); textField. SetText(""); textField. SetHorizontalAlignment(SwingConstants.

LEFT); panel. Add(textField, BorderLayout. SOUTH); textField.

SetColumns(10); textField. SetFocusable(true); setSize(getPreferredSize()); setVisible(true); } /** * @param args */ public static void main(String args) { new KeyboardDispatcherDemo(); } @Override public Dimension getPreferredSize() { // TODO Auto-generated method stub return new Dimension(400,300); } } Thanks to all who nudged me toward the answer. Which brings me to my next question ... stay tuned.

That next question: stackoverflow. Com/questions/6366095/… – Steve Cohen Jun 16 at 1:09.

This application uses a specially remapped keyboard which intercepts the DOS keyboard interrupt (remember that?!) to sometimes alter the scan codes of the keys pressed by the user so that different processing would occur. Special labels were then placed on the keys telling the users the "new" meaning of these keys. The new Java version is required to preserve this keyboard layout which the targeted group of users is very familiar with.

You may never have thought about this, but the numeric keypad of a modern telephone is reversed from the numeric keypad of a computer keyboard. On the former 1-2-3 is on the top row and on the latter it is on the bottom row. We are required to make the keyboard's numeric keypad look like the telephone.

Let's say, when the user types "7" on the numeric keypad, we want it look as though he typed a "1", when he types an "8", we want a "2", when he types a "3" we want a "9". There is much more that we have to do to emulate the DOS application, but we can't even solve this simple case now.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions