Java Code Examples for com.sun.jna.platform.win32.WinUser#INPUT

The following examples show how to use com.sun.jna.platform.win32.WinUser#INPUT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WinUtil.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Forcibly set focus to the given component
 *
 */
public static void requestForeground(Frame frame)
{
	// SetForegroundWindow can't set iconified windows to foreground, so set the
	// frame state to normal first
	frame.setState(Frame.NORMAL);

	User32 user32 = User32.INSTANCE;

	// Windows does not allow any process to set the foreground window, but it will if
	// the process received the last input event. So we send a F22 key event to the process.
	// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow
	WinUser.INPUT input = new WinUser.INPUT();
	input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
	input.input.ki.wVk = new WinDef.WORD(0x85); // VK_F22
	user32.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());

	// Now we may set the foreground window
	WinDef.HWND hwnd = new WinDef.HWND(Native.getComponentPointer(frame));
	user32.SetForegroundWindow(hwnd);
}
 
Example 2
Source File: KeyboardService.java    From winthing with Apache License 2.0 6 votes vote down vote up
public void pressKeys(final List<KeyboardKey> keys) {
    if (keys.isEmpty()) {
        return;
    }

    final WinUser.INPUT input = new WinUser.INPUT();
    final WinUser.INPUT[] inputs = (WinUser.INPUT[]) input.toArray(keys.size() * 2);

    final ListIterator<KeyboardKey> iterator = keys.listIterator();
    int index = 0;
    while (iterator.hasNext()) {
        setKeyDown(inputs[index], iterator.next());
        index++;
    }
    while (iterator.hasPrevious()) {
        setKeyUp(inputs[index], iterator.previous());
        index++;
    }

    user32.SendInput(new WinDef.DWORD(inputs.length), inputs, inputs[0].size());
}
 
Example 3
Source File: WindowsUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Create keyboard input event.
 *
 * @param character character to print
 * @param flags     flags
 * @return keyboard input event
 */
private static WinUser.INPUT createKeyboardInput(char character, long flags) {
    WinUser.INPUT input = new WinUser.INPUT();
    input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
    input.input.setType("ki");
    input.input.ki = new WinUser.KEYBDINPUT();
    input.input.ki.wVk = new WinDef.WORD(0);
    input.input.ki.wScan = new WinDef.WORD(character);
    input.input.ki.time = new WinDef.DWORD(0);
    input.input.ki.dwFlags = new WinDef.DWORD(flags);
    input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR();
    return input;
}
 
Example 4
Source File: WindowsUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Enter a text by sending input events through the Windows API via JNA.
 *
 * @param text text to send through the Windows API
 * @throws WindowsException if the User32 library is not available or no events were processed
 * @see <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendinput">SendInput function</a>
 * @see <a href="https://stackoverflow.com/a/22308727">Using SendInput to send unicode characters beyond U+FFFF</a>
 */
private static void sendTextViaUser32(String text) throws WindowsException {
    if (StringUtils.isEmpty(text))
        return;
    if (USER32 == null)
        throw new WindowsException("User32 library was not loaded.");

    //final List<Long> pointers = new ArrayList<>();
    //noinspection EmptyFinallyBlock
    try {
        final List<WinUser.INPUT> events = new ArrayList<>();
        for (int i = 0; i < text.length(); i++) {
            final char c = text.charAt(i);
            //LOGGER.debug("printing " + c);
            events.add(createKeyboardInput(c, WinUser.KEYBDINPUT.KEYEVENTF_UNICODE));
            events.add(createKeyboardInput(c, WinUser.KEYBDINPUT.KEYEVENTF_KEYUP | WinUser.KEYBDINPUT.KEYEVENTF_UNICODE));
        }
        //for (WinUser.INPUT i : events) {
        //    long address = Pointer.nativeValue(i.getPointer());
        //    if (!pointers.contains(address)) pointers.add(address);
        //}

        WinUser.INPUT[] inputs = events.toArray(new WinUser.INPUT[0]);
        inputs = (WinUser.INPUT[]) inputs[0].toArray(inputs);
        //for (WinUser.INPUT i : inputs) {
        //    long address = Pointer.nativeValue(i.getPointer());
        //    if (!pointers.contains(address)) pointers.add(address);
        //}

        final WinDef.DWORD result = USER32.SendInput(
                new WinDef.DWORD(inputs.length), inputs, inputs[0].size());

        if (result.intValue() < 1) {
            LOGGER.error("last error: {}", Kernel32Util.getLastErrorMessage());
            throw new WindowsException("No events were executed.");
        }
        //LOGGER.debug("result: {}", result.intValue());
    } finally {
        //for (Long address : pointers) {
        //    Kernel32Util.freeLocalMemory(new Pointer(address));
        //}
    }
}
 
Example 5
Source File: KeyboardService.java    From winthing with Apache License 2.0 4 votes vote down vote up
private void setKeyDown(final WinUser.INPUT input, final KeyboardKey key) {
    input.type.setValue(WinUser.INPUT.INPUT_KEYBOARD);
    input.input.setType(WinUser.KEYBDINPUT.class);
    input.input.ki.wVk = key.getVirtualKeyCode();
}
 
Example 6
Source File: KeyboardService.java    From winthing with Apache License 2.0 4 votes vote down vote up
private void setKeyUp(final WinUser.INPUT input, final KeyboardKey key) {
    input.type.setValue(WinUser.INPUT.INPUT_KEYBOARD);
    input.input.setType(WinUser.KEYBDINPUT.class);
    input.input.ki.dwFlags.setValue(WinUser.KEYBDINPUT.KEYEVENTF_KEYUP);
    input.input.ki.wVk = key.getVirtualKeyCode();
}