Sending a Keyboard Input with Java JNA and SendInput()

瘦欲@ 提交于 2019-11-28 06:35:15

问题


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.

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).

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

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

Thanks a lot.

Greetings Ext1nct1on


回答1:


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 );
    }
}


来源:https://stackoverflow.com/questions/28538234/sending-a-keyboard-input-with-java-jna-and-sendinput

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