使用Java JNA和SendInput()发送键盘输入

编程入门 行业动态 更新时间:2024-10-22 12:28:26
本文介绍了使用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: groups.google/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()发送键盘输入

本文发布于:2023-11-07 05:28:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565667.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键盘输入   Java   JNA   SendInput

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!