使用 Alt+Enter 最大化 JavaFX Stage 时删除提示音

我希望我的 JavaFX 应用程序可以通过 Alt+Enter 最大化,所以我添加了:


scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {

    if (event.getCode() == KeyCode.ENTER) {

        if (event.isAltDown()) {

            setFullScreen(!stage.isFullScreen());

            event.consume();

        }

    }

});

然而,当按下 Alt+Enter 时,似乎所有 JavaFX 应用程序默认都会播放“哔”声效果(不知道为什么......)。我将如何继续消除这种声音效果?


RISEBY
浏览 175回答 2
2回答

慕的地8271018

我遇到了同样的问题,并通过使用 Windows API 对窗口进行子类化来解决它。如果你被允许使用 JNA,下面的代码会对你有所帮助。我的用户32.javaimport com.sun.jna.Callback;import com.sun.jna.Native;import com.sun.jna.platform.win32.*;import com.sun.jna.win32.W32APIOptions;public interface MyUser32 extends User32 {&nbsp; &nbsp; int WM_MENUCHAR = 0x0120;&nbsp; &nbsp; int MNC_CLOSE = 1;&nbsp; &nbsp; int VK_RETURN = 0x0d;&nbsp; &nbsp; MyUser32 INSTANCE = Native.loadLibrary("user32", MyUser32.class, W32APIOptions.UNICODE_OPTIONS);&nbsp; &nbsp; LONG_PTR SetWindowLongPtr(WinDef.HWND hWnd, int nIndex, Callback callback);&nbsp; &nbsp; LRESULT CallWindowProc(LONG_PTR lpPrevWndProc, HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam);}App.javaimport com.sun.jna.Pointer;import com.sun.jna.platform.win32.*;import com.sun.jna.platform.win32.WinDef.*;import com.sun.jna.win32.StdCallLibrary;import javafx.application.Application;import javafx.stage.Stage;import java.lang.reflect.Method;public class App extends Application implements StdCallLibrary.StdCallCallback {&nbsp; &nbsp; private BaseTSD.LONG_PTR baseWndProc;&nbsp; &nbsp; public static void main(String[] args) { launch(args); }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage) {&nbsp; &nbsp; &nbsp; &nbsp; // set up scene ...&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; &nbsp; &nbsp; final HWND hWnd = new HWND(getWindowPointer(primaryStage));&nbsp; &nbsp; &nbsp; &nbsp; baseWndProc = MyUser32.INSTANCE.SetWindowLongPtr(hWnd, User32.GWL_WNDPROC, this);&nbsp; &nbsp; }&nbsp; &nbsp; public LRESULT callback(HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam) {&nbsp; &nbsp; &nbsp; &nbsp; if (Msg == MyUser32.WM_MENUCHAR && (wParam.longValue() & 0xffff) == MyUser32.VK_RETURN) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new LRESULT(MyUser32.MNC_CLOSE << 16);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return MyUser32.INSTANCE.CallWindowProc(baseWndProc, hWnd, Msg, wParam, lParam);&nbsp; &nbsp; }&nbsp; &nbsp; private Pointer getWindowPointer(Stage stage) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method getPeer = stage.getClass().getMethod("impl_getPeer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Object tkStage = getPeer.invoke(stage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method getPlatformWindow = tkStage.getClass().getDeclaredMethod("getPlatformWindow");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getPlatformWindow.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Object platformWindow = getPlatformWindow.invoke(tkStage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method getNativeHandle = platformWindow.getClass().getMethod("getNativeHandle");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Pointer((Long) getNativeHandle.invoke(platformWindow));&nbsp; &nbsp; &nbsp; &nbsp; } catch (Throwable t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

GCT1015

我猜你是在 Windows 中编程,因此你得到的哔声是默认的哔声。基于此论坛帖子,似乎ALT+KEY没有找到菜单快捷方式。这很可能是由于Mnemonic Parsing,它允许您将键盘快捷键添加到菜单项,例如ALT+F文件、ALT+S保存等...根据其中一篇文章,Windows 似乎也想获取该组合键,但找不到它,所以它发出哔哔声。我不确定您是否可以添加一个ENTER作为助记符标识符,但您可以通读这篇关于 javafx 助记符的文章,以及关于按钮加速器的这篇文章。我知道第二个是 JavaFx 2.2,但它应该仍然有效。这些应该会给你一些关于如何更彻底地解决这个问题的好主意。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java