简体   繁体   中英

Java Set Text Field Text As Key Pressed

I am working on an auto clicker where a user may set a custom hot key.

I currently have the below function which accepts a key to be assigned as the hotkey and then is supposed to set the text of the hotkey textfield to be the key that was pressed.

I do not want the key's actual name, or it's character. I want it's 'picture' so to speak. So as if I actually typed the key into the text field I want that character to show. So for example, if I want to set the hotkety as the tilde (`) then it should appear like so in the text field.

KeyboardFocusManager.getCurrentKeyboardFocusManager()
                        .addKeyEventDispatcher(new KeyEventDispatcher() {
                            @Override
                            public boolean dispatchKeyEvent(KeyEvent e) {
                                int keycode = e.getKeyCode();
                                hotkeyField.setText("");
                                KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(this);
                                return false;
                            }
                        });

Thanks in advance.

Here is a short code to allow you understand.

I add a KeyListener (in this case a KeyAdapter , which is not an interface but do the same thing) to listen to every key Event on your keybord. Then, you just have to add the key typed in your TextField.

Remember that a KeyListener is only listening in all component which have the listener and focus . This means you need to add the same listener to all your component you want to do this when they have focus.

In the exemple, there is only the label which has the listener. If you click on the JButton, nothing will happen then.

    import java.awt.Dimension;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class FieldListener{

        public FieldListener(){
            this.initGUI();
        }

        private void initGUI(){

            //Create a frame with a JLabel and a JTextField
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            JTextField field = new JTextField();
            field.setPreferredSize(new Dimension(80,30));
            JLabel label = new JLabel("Shortcut :");
            label.setFocusable(true); //Label must be focusable

            panel.add(label);
            panel.add(field);

            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);

            label.requestFocus(); //Set focus on the JLabel to unfocus the JTextField

            //Listen every key event on the label
            label.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e){
                    //Set the text with the key typed
                    field.setText(KeyEvent.getKeyText(e.getKeyCode())); 
                }
            });
        }

        public static void main(String[] args) {
            new FieldListener();
        }   
    }

I hope it helped you !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM