views:

204

answers:

1

Hello All,

I am making a program that allows user to custom keyboard shortcuts, for this i need the available keys to be displayed, what is the best way to achieve this in java swing?

    KeyEvent.class.getDeclaredFields()

I am intrested in dynamic example of below,

keysLST.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CTRL", "ALT", "SHIFT" }));

one way i know is to get all from the keyevent class but i am not sure how to integrate it to the list. any help would be appreciated.

Thanks

A: 

Instead of picking from a list, add a KeyListener to any component (a JTextField works) and record the key code as the user presses the key. You can also record modifiers (ctrl,alt,shift) this way.

public void keyPressed(KeyEvent e)
{
    int keyTheUserJustPressed = e.getKeyCode();

    // then use for ctrl/alt/shift
    e.getModifiersEx();

    // or use
    e.isAltDown();
    e.isShiftDown();
}
basszero
Hi basszero ,i want to keep these choice as they will be used later by a robot, the robot will sending the keys it reads from the list box or from default is none is selected.
xusenm
then save the e.getKeyCode() into some structure outside of the listener
basszero
On further investigation i believe your orignal idea was good one, it is easier for the user to click the keys than to go through a huge list. thanks for the help!
xusenm