使用Java JNA和SendInput()发送键盘输入 [英] Sending a Keyboard Input with Java JNA and SendInput()

查看:320
本文介绍了使用Java JNA和SendInput()发送键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我想与使用Java的OS与Windows 7进行交互,并希望在较低级别上模拟一些按键(例如CTRL + V).

I'm interested in interacting with the OS with java in this case windows 7 and want to emulate some keystrokes (e.g. CTRL + V) on a low level.

首先,我知道Java是一个不好的选择,但是它是我最好的编程语言,我知道它是可能的. 另外,我知道awt.robot存在,但对于我来说它的级别太高了(我真的需要驱动程序级别).

First of all i know java is a bad choice but its my best programming language and i know its possible. Additionally i know awt.robot exists but its too high level for me (i really need the driver level).

我问这个问题是因为我真的很想了解jna,并且在观看了20个代码示例后仍然遇到问题.

I'm asking this question because I really want to understand jna and after watching 20 code examples im still having problems.

一个由sendInput()进行击键的代码示例确实会对我有所帮助.

A code example for a keystroke done by sendInput() would really help me.

非常感谢.

问候Ext1nct1on

Greetings Ext1nct1on

推荐答案

如果JNA SendInput仍有问题,请看以下示例:

If somebody has still problems with JNA SendInput look at this example:

import com.sun.jna.Native;
import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;

/**
 * Created by Vellotis on 2.02.2016.
 */
public class User32Test
{
    public static void main( String[] args )
    {
        // Loop all windows
        User32.INSTANCE.EnumWindows(( hWnd, data ) -> {
            char[] name = new char[512];

            User32.INSTANCE.GetWindowText( hWnd, name, name.length );

            // Find window with title starting with downcase "keyb" string
            if ( Native.toString( name ).toLowerCase().startsWith( "keyb" ) )
            {
                // Bring the window to the front
                User32.INSTANCE.SetForegroundWindow( hWnd );

                // Prepare input reference
                WinUser.INPUT input = new WinUser.INPUT(  );

                input.type = new WinDef.DWORD( WinUser.INPUT.INPUT_KEYBOARD );
                input.input.setType("ki"); // Because setting INPUT_INPUT_KEYBOARD is not enough: https://groups.google.com/d/msg/jna-users/NDBGwC1VZbU/cjYCQ1CjBwAJ
                input.input.ki.wScan = new WinDef.WORD( 0 );
                input.input.ki.time = new WinDef.DWORD( 0 );
                input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR( 0 );

                // Press "a"
                input.input.ki.wVk = new WinDef.WORD( 'A' ); // 0x41
                input.input.ki.dwFlags = new WinDef.DWORD( 0 );  // keydown

                User32.INSTANCE.SendInput( new WinDef.DWORD( 1 ), ( WinUser.INPUT[] ) input.toArray( 1 ), input.size() );

                // Release "a"
                input.input.ki.wVk = new WinDef.WORD( 'A' ); // 0x41
                input.input.ki.dwFlags = new WinDef.DWORD( 2 );  // keyup

                User32.INSTANCE.SendInput( new WinDef.DWORD( 1 ), ( WinUser.INPUT[] ) input.toArray( 1 ), input.size() );

                return false; // Found
            }

            return true; // Keep searching
        }, null );
    }
}

这篇关于使用Java JNA和SendInput()发送键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆