Java Swing fullscreen with keyboard input on Mac OS X

£可爱£侵袭症+ 提交于 2019-12-24 11:08:04

问题


I'm having some issues getting my Java JFrame to be fullscreen on all OS (Windows, Mac, Linux). It seems whatever solution I find it does run on one OS but not on others or has some other serious bugs. I wanted to use the setFullScreenWindow(window w) method to properly initiate a fullscreen because setExtendedState(...) won't work on Mac/Linux as the menubar and taskbar are still visible.

The setFullScreenWindow(...) method worked fine on all environments until Java 7 came along and now there seems to be an issue that as soon as you enter fullscreen mode the application no longer responds to key events on Mac OS X. The application works just fine on Windows.

Does anyone have a clue how I could possibly work around this issue?

Note: The workaround described here (FullScreen Swing Components Fail to Receive Keyboard Input on Java 7 on Mac OS X Mountain Lion) does not work for Windows because it will result in the JFrame flickering and not opening properly.

The fullscreen approach described here is the same used below which does not work because of problems with the key input: (How to make a JFrame really fullscreen?)

Example Code:

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class FullScreenKeyTest extends JFrame {

public void createFrame() {
    initKey();
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    this.setUndecorated(true);
    this.setVisible(true);
    gd.setFullScreenWindow(this);
}

private void initKey() {
    Action keyAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Escape key pressed");
            setVisible(false);
            System.exit(0);
        }
    };
    this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "keyPress");
    this.getRootPane().getActionMap().put("keyPress", keyAction);
}

public static void main(String[] args) {
    FullScreenKeyTest testFrame = new FullScreenKeyTest();
    testFrame.createFrame();
}
}

回答1:


This is a bit flaky as I can get it to work and break it at the same time.

With your example code, I added

getContentPane().setFocusable(true);
getContentPane().requestFocus();

To the end of the createFrame method, and instead of registering the action against the root pane, I registered against the content pane




回答2:


I found that keyboard inputs work if you use the native OS X Lion fullscreen API. See solution to the question Fullscreen feature for Java Apps on OSX Lion.



来源:https://stackoverflow.com/questions/14191776/java-swing-fullscreen-with-keyboard-input-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!