Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Win+R from opening run tool

Tags:

In my javafx program is a popup which lets user press keys and then it sets label accordingly. My problem is with key combinations that are shortcuts for underlying OS for example if user presses Win+R then Run.exe starts but my program should just set the label to "Win+R". My question is how to stop keyevents from triggering OS shortcuts.

Here is the relevant code.

public void showInput() {
        Set codes = new HashSet();

        Stage inputWindow = new Stage();
        GridPane pane = new GridPane();
        Scene scene = new Scene(pane);
        Label label = new Label("Here comes the pressed keys");

        scene.setOnKeyPressed(e -> {
            e.consume();
            int code = e.getCode().ordinal();

            if (label.getText().equals("Here comes the pressed keys")){
                codes.add(code);
                label.setText(String.valueOf(e.getCode().getName()));

            } else if (!codes.contains(code)){
                codes.add(code);
                label.setText(label.getText() + "+" + e.getCode().getName());
            }
        });

        scene.setOnKeyReleased(e -> {
            e.consume();
            inputWindow.close();
        });

        pane.add(label, 0, 0);

        inputWindow.setScene(scene);
        inputWindow.show();
    }

I tried e.consume() but it did not help.

like image 575
griips21 Avatar asked Jul 16 '17 20:07

griips21


People also ask

How do I disable Run box?

Way 1: Disable Run dialog box using Group Policy Editor On the right side pane, scroll down to look for the entry named "Remove Run menu from Start Menu", and then double-click this entry to configure it, which is not configured by default. Step 3: Select Enabled, and click Apply followed by OK.

How do I stop a Run command?

Then we hit Ctrl+C to terminate the execution.

What is Win R on Windows?

The Windows + R will show you the "RUN" box where you can type commands to either pull up a program or go online. The Windows key is the one in the middle of CTRL and ALT on the lower left side. The R key is the one that is located between the "E" and "T" key. Hope that helps.


2 Answers

It's possible with JNA, but is a bad idea. Don't intercept well-known key combinations.

Nevertheless, below is a working example. It basically uses the SetWindowsHookEx Win32 API and then blocks the Win+R key combination in the hook callback.

import com.sun.jna.platform.win32.*;

public class Test {

    public static User32.HHOOK hHook;
    public static User32.LowLevelKeyboardProc lpfn;

    public static void main(String[] args) throws Exception {
        WinDef.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
        lpfn = new User32.LowLevelKeyboardProc() {
            boolean winKey = false;
            public WinDef.LRESULT callback(int nCode, WinDef.WPARAM wParam, WinUser.KBDLLHOOKSTRUCT lParam) {
                if (lParam.vkCode == 0x5B)
                    winKey = (lParam.flags & 0x80) == 0;
                if (lParam.flags == 0 && lParam.vkCode == 0x52 && winKey) {
                    System.out.println("Win-R pressed");
                    return new WinDef.LRESULT(-1);
                }
                return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
            }
        };
        hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod, 0);
        if (hHook == null) {
            System.out.println("Unable to set hook");
            return;
        }
        User32.MSG msg = new User32.MSG();
        while (User32.INSTANCE.GetMessage(msg, null, 0, 0) != 0) {
        }
        if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
            System.out.println("Unhooked");
    }
}

(The needed JNA JAR dependency is net.java.dev.jna : platform)

like image 158
rustyx Avatar answered Sep 20 '22 20:09

rustyx


Not possible, Java layer is above OS layer meaning your code is handled by the JVM and the JVM is handled by the OS. So there is no way to "skip" the OS layer and send your commands directly to Java.

like image 41
Shell_Leko Avatar answered Sep 23 '22 20:09

Shell_Leko