Skip to main content

Any way to capture key events for configuration

4 replies [Last post]
i30817
Offline
Joined: 2006-05-02
Points: 0

You know like those programs that go click here to configurate xxxx. I'd only need a modified jlabel that could do that (hum thats a nice idea for swinglabs). Anyway the idea is that it would InputMap compatible keycodes, you know : "released RIGHT" "typed o" things like that, to configurate a keymap.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
i30817
Offline
Joined: 2006-05-02
Points: 0

I've done a little prototype:

The obvious problem is that it can't detect a outside click for when the user clicks outside of the component (even if the clicked area is not a focusable component). How to resolve this? If you say glasspane you're going to make me cry.

Edit: here is a solution. I didn't know that swing had a global event listener. This probably should be said in the focus tutorial. A pity about it requiring permissions. Why BTW since we can add a input listener to a normal component withou permissions right? |:|)

Edit2 : Did a little suffling around to make the global listener only active when capturing keycodes and making the fireProperty whenever the keyStroke changes.
Also it seems that the system colors are not updated by the look and feel in highcontrast mode, windows xp, java 1.6 ?

Edit3 : removed a little key duplication in the toString(). There are still unmappable chars like («) and (ç) and probably more. If you know how to recognize and fix that reply please.

Edit 4 : fixing a little code uglyness.

Edit 5 : Oh well. When you cant resolve something because its native problem, just never let it happen anyway. Set a check on setKeyCode, to not allow key codes that are not recognized by the java input system.

[code]
/*
* KeyCapturer.java
*
* Created on 24 de Fevereiro de 2007, 19:36
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package ui;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

/**
*
* @author i30817
*/
public class KeyCapturer extends JLabel{

/**
* Field used to listen to property changed events refering to changed
* captured keys.
**/
public static String CAPTURED_KEYSTROKE = "Captured a keyStroke";

private static KeyStroke defaultCancelKeyStroke = KeyStroke.getKeyStroke("ESCAPE");
private KeyStroke capturedKeyStroke;
private boolean capturing = false;

private AWTEventListener mouseTrick = new AWTEventListener(){
public void eventDispatched(AWTEvent e){
if (e instanceof MouseEvent && capturing) {
MouseEvent me = (MouseEvent)e;
if (me.getClickCount() > 0) {
getParent().requestFocus();
}
}
}
};

private static FocusListener focus = new FocusAdapter(){
Color c = SystemColor.controlShadow;
public void focusGained(FocusEvent e) {
if( !e.isTemporary() ){
KeyCapturer thisOne = (KeyCapturer) e.getComponent();
thisOne.capturing = true;
thisOne.setBackground(c);
Toolkit.getDefaultToolkit().addAWTEventListener(thisOne.mouseTrick, AWTEvent.MOUSE_EVENT_MASK);
}
}
public void focusLost(FocusEvent e) {
if( !e.isTemporary() ){
KeyCapturer thisOne = (KeyCapturer) e.getComponent();
Toolkit.getDefaultToolkit().removeAWTEventListener(thisOne.mouseTrick);
thisOne.capturing = false;
thisOne.setBackground(null);
}
}
};
private static MouseListener mouse = new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
KeyCapturer thisOne = (KeyCapturer) e.getComponent();
thisOne.requestFocusInWindow();
}
};
private KeyListener key = new KeyAdapter(){

public void keyPressed(KeyEvent arg0) {

if(capturing){
KeyStroke key = KeyStroke.getKeyStrokeForEvent(arg0);

if(key.equals(defaultCancelKeyStroke)){
getParent().requestFocus();
return;
}
else if(!key.equals(capturedKeyStroke)){

setKeyStroke(key);
}
}
}
};

public void setKeyStroke(KeyStroke k){
if(k.getKeyCode() != KeyEvent.VK_UNDEFINED){
KeyStroke old = capturedKeyStroke;
capturedKeyStroke = k;
setText(toString());
firePropertyChange(CAPTURED_KEYSTROKE, old, capturedKeyStroke);
}
}

public KeyStroke getKeyStroke(){
return capturedKeyStroke;
}

public String toString(){
int code = capturedKeyStroke.getKeyCode();
char charCode = capturedKeyStroke.getKeyChar();
int modifier = capturedKeyStroke.getModifiers();
String mod = KeyEvent.getKeyModifiersText(modifier);
String key;

if (KeyEvent.CHAR_UNDEFINED == charCode) {
key = KeyEvent.getKeyText(code).toLowerCase();
} else {
key = String.valueOf(charCode).toLowerCase();
}

return (mod.equalsIgnoreCase(key))? mod : mod +" "+ key;
}

private void setup(KeyStroke cancelCaptureKey){
if(cancelCaptureKey == null)
defaultCancelKeyStroke = cancelCaptureKey;
addKeyListener(key);
addFocusListener(focus);
addMouseListener(mouse);
setOpaque(true);
}
public KeyCapturer(String text, Icon icon, int horizontalAlignment, KeyStroke cancelCaptureKey) {
super(text, icon, horizontalAlignment);
setup(cancelCaptureKey);
}
public KeyCapturer(String text, int horizontalAlignment, KeyStroke cancelCaptureKey) {
super(text, horizontalAlignment);
setup(cancelCaptureKey);
}
public KeyCapturer(String text, KeyStroke cancelCaptureKey) {
super(text);
setup(cancelCaptureKey);
}
public KeyCapturer(Icon image, int horizontalAlignment, KeyStroke cancelCaptureKey) {
super(image, horizontalAlignment);
setup(cancelCaptureKey);
}
public KeyCapturer(Icon image, KeyStroke cancelCaptureKey) {
super(image);
setup(cancelCaptureKey);
}
public KeyCapturer(KeyStroke cancelCaptureKey){
super();
setup(cancelCaptureKey);
}
public KeyCapturer() {
super();
setup(null);
}
}
[/code]

Message was edited by: i30817

Message was edited by: i30817

Message was edited by: i30817

Message was edited by: i30817

Message was edited by: i30817

Joshua Marinacci

This might do what you want:

http://www.jroller.com/page/santhosh?entry=reading_shortcuts

I recently adapted this to a program that lets you define the
keyboard shortcut (accelerator in Swing parlance) and it works quite
well.

- Josh

On Feb 24, 2007, at 5:36 PM, jdnc-interest@javadesktop.org wrote:

> I've done a little prototype:
>
> The obvious problem is that it can't detect a outside click for
> when the user clicks outside of the component (even if the clicked
> area is not a focusable component). How to resolve this? If you say
> glasspane you're going to make me cry.
>
> [code]
> /*
> * KeyCapturer.java
> *
> * Created on 24 de Fevereiro de 2007, 19:36
> *
> * To change this template, choose Tools | Template Manager
> * and open the template in the editor.
> */
>
> package ui;
>
> import java.awt.Color;
> import java.awt.SystemColor;
> import java.awt.event.FocusEvent;
> import java.awt.event.FocusListener;
> import java.awt.event.KeyAdapter;
> import java.awt.event.KeyEvent;
> import java.awt.event.KeyListener;
> import java.awt.event.MouseAdapter;
> import java.awt.event.MouseEvent;
> import java.awt.event.MouseListener;
> import javax.swing.Icon;
> import javax.swing.JLabel;
> import javax.swing.KeyStroke;
>
> /**
> *
> * @author i30817
> */
> public class KeyCapturer extends JLabel{
>
> /**
> * Field used to listen to property changed events refering to
> changed
> * captured keys.
> **/
> public static String CAPTURED_KEYSTROKE = "Captured a keyStroke";
>
> private static KeyStroke defaultCancelKeyStroke =
> KeyStroke.getKeyStroke("ESC");
> private KeyStroke capturedKeyStroke;
> private boolean capturing = false;
>
>
> private FocusListener focus = new FocusListener(){
> Color c = (SystemColor.controlHighlight == null) ?
> Color.BLUE : SystemColor.controlHighlight;
> public void focusGained(FocusEvent e) {
> if( !e.isTemporary() ){
> capturing = true;
> setBackground(c);
> setOpaque(true);
> }
> }
> public void focusLost(FocusEvent e) {
> if( !e.isTemporary() ){
> capturing = false;
> setBackground(null);
> setOpaque(false);
> }
> }
> };
> private MouseListener mouse = new MouseAdapter(){
> public void mouseClicked(MouseEvent e) {
> requestFocusInWindow();
> }
> };
> private KeyListener key = new KeyAdapter(){
>
> public void keyPressed(KeyEvent arg0) {
>
> if(capturing){
> KeyStroke key = KeyStroke.getKeyStrokeForEvent(arg0);
>
>
> if(key.equals(defaultCancelKeyStroke)){
> getParent().requestFocus();
> }
> else if(!key.equals(capturedKeyStroke)){
> KeyCapturer.this.firePropertyChange
> (CAPTURED_KEYSTROKE, capturedKeyStroke, key);
> setKeyStroke(key);
> }
> }
> }
> };
>
>
> public void setKeyStroke(KeyStroke k){
> capturedKeyStroke = k;
> setText(toString());
> }
>
> public KeyStroke getKeyStroke(){
> return capturedKeyStroke;
> }
>
> public String toString(){
> int code = capturedKeyStroke.getKeyCode();
> char charCode = capturedKeyStroke.getKeyChar();
> int modifier = capturedKeyStroke.getModifiers();
>
> if (KeyEvent.CHAR_UNDEFINED == charCode) {
> return KeyEvent.getKeyModifiersText(modifier) + ("
> "+KeyEvent.getKeyText(code)).toUpperCase();
> } else {
> return KeyEvent.getKeyModifiersText(modifier) + ("
> "+charCode).toUpperCase();
> }
>
> }
>
>
> public KeyCapturer(String text, Icon icon, int
> horizontalAlignment) {
> super(text, icon, horizontalAlignment);
> addKeyListener(key);
> addFocusListener(focus);
> }
>
> public KeyCapturer(String text, Icon icon, int
> horizontalAlignment, KeyStroke cancelCaptureKey) {
> super(text, icon, horizontalAlignment);
> addKeyListener(key);
> addFocusListener(focus);
> addMouseListener(mouse);
> if(cancelCaptureKey == null)
> throw new IllegalArgumentException("The cancel
> keystroke cannot be null");
> defaultCancelKeyStroke = cancelCaptureKey;
> }
>
> public KeyCapturer(String text, int horizontalAlignment) {
> super(text, horizontalAlignment);
> addKeyListener(key);
> addFocusListener(focus);
> addMouseListener(mouse);
> }
> public KeyCapturer(String text) {
> super(text);
> addKeyListener(key);
> addFocusListener(focus);
> addMouseListener(mouse);
> }
> public KeyCapturer(Icon image, int horizontalAlignment) {
> super(image, horizontalAlignment);
> addKeyListener(key);
> addFocusListener(focus);
> addMouseListener(mouse);
> }
> public KeyCapturer(Icon image) {
> super(image);
> addKeyListener(key);
> addFocusListener(focus);
> addMouseListener(mouse);
> }
> public KeyCapturer() {
> super();
> addKeyListener(key);
> addFocusListener(focus);
> addMouseListener(mouse);
> }
>
>
>
>
>
>
> }
> [/code]
> [Message sent by forum member 'i30817' (i30817)]
>
> http://forums.java.net/jive/thread.jspa?messageID=205128
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

- Blasting forth in three part harmony!

[att1.html]

i30817
Offline
Joined: 2006-05-02
Points: 0

Both versions have a little problem that some (legal) character are unmappable to text. Like («). Yah for unicode support eh?

i30817
Offline
Joined: 2006-05-02
Points: 0

Btw way you've replied with an old version of the post, (and i'm replying and not editing to try to avoid any bug that might exist in the board software.)