Example usage for java.awt AWTEvent KEY_EVENT_MASK

List of usage examples for java.awt AWTEvent KEY_EVENT_MASK

Introduction

In this page you can find the example usage for java.awt AWTEvent KEY_EVENT_MASK.

Prototype

long KEY_EVENT_MASK

To view the source code for java.awt AWTEvent KEY_EVENT_MASK.

Click Source Link

Document

The event mask for selecting key events.

Usage

From source file:Main.java

/**
 * Adds a global Keylistener which receive all KeyEvents which are sent.</br> Code
 * Example:</br> <code><pre>
 * AWTEventListener ael = new AWTEventListener() { 
 *    public void eventDispatched(AWTEvent e ) { 
 *       if(e.getID() == KeyEvent.KEY_PRESSED){
 *          doSomeThing(); //eg. keyPressed((KeyEvent) e);  
 *       }  /*from www . j  ava2s . c  om*/
 *    }
 * };
 * </pre>
 *</code>
 * 
 * @param listener to add
 */
public static void addGlobalKeyListener(AWTEventListener listener) {
    Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);
}

From source file:Main.java

/**
 * Adds a glass layer to the dialog to intercept all key events. If the
 * espace key is pressed, the dialog is disposed (either with a fadeout
 * animation, or directly)./*from   w w w . ja v a2 s. c  o  m*/
 */
public static void addEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose) {
    LayerUI<Container> layerUI = new LayerUI<Container>() {
        private boolean closing = false;

        @Override
        public void installUI(JComponent c) {
            super.installUI(c);
            ((JLayer) c).setLayerEventMask(AWTEvent.KEY_EVENT_MASK);
        }

        @Override
        public void uninstallUI(JComponent c) {
            super.uninstallUI(c);
            ((JLayer) c).setLayerEventMask(0);
        }

        @Override
        public void eventDispatched(AWTEvent e, JLayer<? extends Container> l) {
            if (e instanceof KeyEvent && ((KeyEvent) e).getKeyCode() == KeyEvent.VK_ESCAPE) {
                if (closing)
                    return;
                closing = true;
                if (fadeOnClose)
                    fadeOut(dialog);
                else
                    dialog.dispose();
            }
        }
    };

    JLayer<Container> layer = new JLayer<>(dialog.getContentPane(), layerUI);
    dialog.setContentPane(layer);
}

From source file:Main.java

/**
 * Adds a glass layer to the dialog to intercept all key events. If the
 * espace key is pressed, the dialog is disposed (either with a fadeout
 * animation, or directly)./*from   w w  w .j  a va 2  s  .  co  m*/
 */
public static void addEscapeToCloseSupport(final JDialog dialog) {
    LayerUI<Container> layerUI = new LayerUI<Container>() {

        private boolean closing = false;

        @Override
        public void installUI(JComponent c) {
            super.installUI(c);
            ((JLayer) c).setLayerEventMask(AWTEvent.KEY_EVENT_MASK);
        }

        @Override
        public void uninstallUI(JComponent c) {
            super.uninstallUI(c);
            ((JLayer) c).setLayerEventMask(0);
        }

        @Override
        public void eventDispatched(AWTEvent e, JLayer<? extends Container> l) {
            if (e instanceof KeyEvent && ((KeyEvent) e).getKeyCode() == KeyEvent.VK_ESCAPE) {
                if (closing) {
                    return;
                }
                closing = true;
                fadeOut(dialog);
            }
        }
    };

    JLayer<Container> layer = new JLayer<Container>(dialog.getContentPane(), layerUI);
    dialog.setContentPane(layer);
}

From source file:Main.java

static void setLightweightDispatcher(JComponent component) {
    if (dispatcherField == null) {
        initReflection();/*from   w w w . j  a  v a 2s  .  c  o m*/
    }
    try {
        Object dispatcher = newLightweightDispatcher.newInstance(component);
        enableEvents.invoke(dispatcher, MOUSE_MASK | AWTEvent.KEY_EVENT_MASK);
        dispatcherField.set(component, dispatcher);
        component.addKeyListener(new KeyAdapter() {
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/** The constructor: register the event types we are interested in */
public Main() {//from ww  w  .  j a v a  2s. co m
    // We're interested in all types of events
    this.enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK
            | AWTEvent.FOCUS_EVENT_MASK | AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
    this.setPreferredSize(new Dimension(500, 400));
}

From source file:EventTestPane.java

/** The constructor: register the event types we are interested in */
public EventTestPane() {
    // We're interested in all types of events
    this.enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK
            | AWTEvent.FOCUS_EVENT_MASK | AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
    this.setPreferredSize(new Dimension(500, 400));
}

From source file:Main.java

public Main() {
    label = new JLabel("Waiting...");
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(label);/*from w ww. j  a v  a 2  s .  c o m*/
    frame.setSize(200, 200);
    frame.setVisible(true);

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        int count;

        @Override
        public void eventDispatched(AWTEvent event) {
            Object source = event.getSource();
            if (source instanceof Component) {
                Component comp = (Component) source;
                Window win = null;
                if (comp instanceof Window) {
                    win = (Window) comp;
                } else {
                    win = SwingUtilities.windowForComponent(comp);
                }
                if (win == frame) {
                    timer.restart();
                    label.setText("Interrupted..." + (++count));
                }
            }
        }
    }, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK
            | AWTEvent.MOUSE_WHEEL_EVENT_MASK);

    timer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });
    timer.start();
}

From source file:com.jcraft.weirdx.DDXWindowImp.java

public void init(XWindow w) {
    if (serverClient == null) {
        ddxwindow = w.screen.root.ddxwindow;
        threeButton = WeirdX.threeButton;
        serverClient = w.screen.root.client;
    }//from  ww  w.j a v a 2s  .com
    this.window = w;
    this.bw = w.borderWidth;
    bwc = true;
    setSize(w.width, w.height);
    addMouseListener(this);
    addMouseMotionListener(this);
    //addKeyListener(this);
    px = 0;
    py = 0;
    enableEvents(AWTEvent.KEY_EVENT_MASK);
    setVisible(false); //???
    setLayout(null);

    clck_toggle = false;
}

From source file:com.brainflow.application.toplevel.Brainflow.java

private void initializeToolBar() {

    CommandGroup mainToolbarGroup = new CommandGroup("main-toolbar");
    mainToolbarGroup.bind(getApplicationFrame());

    ToggleGroup interpToggleGroup = new ToggleGroup("toggle-interp-group");
    interpToggleGroup.bind(getApplicationFrame());

    OpenImageCommand openImageCommand = new OpenImageCommand();
    openImageCommand.bind(getApplicationFrame());

    SnapshotCommand snapshotCommand = new SnapshotCommand();
    snapshotCommand.bind(getApplicationFrame());

    CreateAxialViewCommand axialCommand = new CreateAxialViewCommand();
    axialCommand.bind(getApplicationFrame());
    axialCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateSagittalViewCommand sagittalCommand = new CreateSagittalViewCommand();
    sagittalCommand.bind(getApplicationFrame());
    sagittalCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateCoronalViewCommand coronalCommand = new CreateCoronalViewCommand();
    coronalCommand.bind(getApplicationFrame());
    coronalCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateVerticalOrthogonalCommand vertCommand = new CreateVerticalOrthogonalCommand();
    vertCommand.bind(getApplicationFrame());
    vertCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateHorizontalOrthogonalCommand horizCommand = new CreateHorizontalOrthogonalCommand();
    horizCommand.bind(getApplicationFrame());
    horizCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateTriangularOrthogonalCommand triCommand = new CreateTriangularOrthogonalCommand();
    triCommand.bind(getApplicationFrame());
    triCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CommandGroup orthoGroup = new CommandGroup("ortho-view-group");
    orthoGroup.bind(getApplicationFrame());

    final NextSliceCommand nextSliceCommand = new NextSliceCommand();
    nextSliceCommand.bind(getApplicationFrame());
    nextSliceCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    final PreviousSliceCommand previousSliceCommand = new PreviousSliceCommand();
    previousSliceCommand.bind(getApplicationFrame());
    previousSliceCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent event) {
            if (event.getID() == KeyEvent.KEY_PRESSED) {
                KeyEvent ke = (KeyEvent) event;
                if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
                    previousSliceCommand.execute();
                } else if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
                    nextSliceCommand.execute();
                }// w  w  w  .j  a v  a  2  s . c  o  m

            }

        }
    }, AWTEvent.KEY_EVENT_MASK);

    PageBackSliceCommand pageBackSliceCommand = new PageBackSliceCommand();
    pageBackSliceCommand.bind(getApplicationFrame());
    pageBackSliceCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    PageForwardSliceCommand pageForwardSliceCommand = new PageForwardSliceCommand();
    pageForwardSliceCommand.bind(getApplicationFrame());
    pageForwardSliceCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    IncreaseContrastCommand increaseContrastCommand = new IncreaseContrastCommand();
    increaseContrastCommand.bind(getApplicationFrame());
    increaseContrastCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    DecreaseContrastCommand decreaseContrastCommand = new DecreaseContrastCommand();
    decreaseContrastCommand.bind(getApplicationFrame());
    decreaseContrastCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    ToggleCommand nearest = new NearestInterpolationToggleCommand();
    nearest.bind(getApplicationFrame());
    decreaseContrastCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    ToggleCommand linear = new LinearInterpolationToggleCommand();
    linear.bind(getApplicationFrame());

    ToggleCommand cubic = new CubicInterpolationToggleCommand();
    cubic.bind(getApplicationFrame());

    ToggleCommand toggleAxisLabelCommand = new ToggleAxisLabelCommand();
    toggleAxisLabelCommand.bind(getApplicationFrame());

    JToolBar mainToolbar = mainToolbarGroup.createToolBar();
    //ActionCommand increaseContrastCommand = new IncreaseContrastCommand();
    //increaseContrastCommand.bind(brainFrame);
    //mainToolbar.add(increaseContrastCommand.getActionAdapter());

    //ActionCommand decreaseContrastCommand = new DecreaseContrastCommand();
    //decreaseContrastCommand.bind(brainFrame);
    //mainToolbar.add(decreaseContrastCommand.getActionAdapter());

    brainFrame.getContentPane().add(mainToolbar, BorderLayout.NORTH);

    //InputMap map = documentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

    //for (KeyStroke ks : map.keys()) {
    //    System.out.println("key : " + ks);
    //}

}

From source file:idontwant2see.IDontWant2See.java

@Override
public void onActivation() {
    mFilter = new PluginsProgramFilter(this) {
        public String getSubName() {
            return "";
        }/*from  ww  w. j av a  2  s .c om*/

        public boolean accept(final Program prog) {
            return acceptInternal(prog);
        }
    };

    Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
}