CTRL + ENTER not working in RSyntaxTextArea

Questions on using RSyntaxTextArea should go here.

Moderator: robert

CTRL + ENTER not working in RSyntaxTextArea

Postby Endogen » Wed Jan 22, 2014 9:05 pm

I added the KeyEvent CTRL + ENTER to the InputMap of RSyntaxTextArea but that doesn't work. In a normal JTextField it works fine. Any idea how i can get this working?

Example:
Code: Select all
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rtextarea.RTextScrollPane;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class KeyTest extends JFrame {

    public static void main(String args[]) {
        new KeyTest().setVisible(true);
    }

    public KeyTest() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setPreferredSize(new Dimension(500, 400));
        pack();
        setLocationRelativeTo(null);

        init();
    }

    private void init() {
        JTextField textField = new JTextField();
        add(textField, BorderLayout.NORTH);

        RSyntaxTextArea textArea = new RSyntaxTextArea();
        RTextScrollPane scrollPane = new RTextScrollPane(textArea);
        this.add(scrollPane, BorderLayout.CENTER);

        Action action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("WORKS!");
            }
        };

        addCtrlEnter(textField, action);
        addCtrlEnter(textArea, action);
    }

    private void addCtrlEnter(JComponent component, Action action) {
        component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK), "CtrlEnter");
        component.getActionMap().put("CtrlEnter", action);
    }
}
Endogen
 
Posts: 5
Joined: Sat Jan 04, 2014 10:18 am

Re: CTRL + ENTER not working in RSyntaxTextArea

Postby Blogghete » Thu Jan 23, 2014 8:49 am

Example of RunAction.java:
Code: Select all
package net.sourceforge.keytest.actions;

/**
 *
 *
 * @author Blogghete
 *
 */
public class RunAction extends StandardAction {

    private static final long serialVersionUID = -502970390309220470L;

    protected RunAction() {
        super("Run", "run.png", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), null);
        boolean isConnected = Context.getInstance().getConnectionData() != null;
        setEnabled(isConnected);
    }

    @Override
    protected void performThreaded(ActionEvent ae) throws Exception {
        // The action to perform when the user press CTRL + ENTER
    }
}


Example of Actions.java
Code: Select all
package net.sourceforge.keytest.actions;

/**
 *
 *
 * @author Blogghete
 *
 */
public final class Actions {

    public static final StandardAction RUN = new RunAction();

    private static final Actions ACTIONS = new Actions();

    private Actions() {
    }

    public static Actions getInstance() {
        return ACTIONS;
    }

    protected void validateActions() {
        boolean isConnected = Context.getInstance().getConnectionData() != null;
        RUN.setEnabled(isConnected);
    }
}


Example of RTADefaultInputMap.java
Code: Select all
package net.sourceforge.keytest.editor;

/**
 * The default input map for a <code>RTextArea</code>.
 *
 * @author Blogghete
 *
 */
public class RTADefaultInputMap extends InputMap {

    private static final long serialVersionUID = -2883392214711226260L;

    /** Constructs the default input map for an <code>RTextArea</code>. */
    public TADefaultInputMap() {
        super();

        int defaultModifier = getDefaultModifier();
        int alt = InputEvent.ALT_MASK;
        int shift = InputEvent.SHIFT_MASK;

        put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, defaultModifier),
                Actions.RUN);
    }

    /**
     * Returns the default modifier key for a system. For example, on Windows
     * this would be the CTRL key (<code>InputEvent.CTRL_MASK</code>).
     *
     * @return The default modifier key.
     */
    protected static final int getDefaultModifier() {
        return Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    }
}
Blogghete
 
Posts: 9
Joined: Wed May 29, 2013 3:25 pm

Re: CTRL + ENTER not working in RSyntaxTextArea

Postby Endogen » Thu Jan 23, 2014 10:01 pm

I got the wrong InputMap. Here's what went wrong:

Doesn't work:
Code: Select all
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(...


Works:
Code: Select all
.getInputMap().put(...
Endogen
 
Posts: 5
Joined: Sat Jan 04, 2014 10:18 am


Return to Help

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests

cron