我正在创建一个存储密码的软件,以便可以将其发送到令牌的登录窗口。问题是某些字符如“ @”和“〜”无法发送。
为了向您展示问题,我将目标窗口从令牌的登录窗口更改为记事本编辑器的窗口。如果有人要运行下面的代码,则会看到“ x”字符发送到记事本,但“ @”不是。
import java.util.HashMap;
import java.util.Map;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.Pointer;
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.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
public class Main {
public static final int MAX_TITLE_LENGTH = 1024;
public static void main(String[] args) {
Map<HWND, String> windows = getWindows();
for (Map.Entry<HWND,String> window : windows.entrySet()) {
if (window.getValue().contains("Notepad")) {
User32.INSTANCE.SetForegroundWindow( window.getKey() );
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 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 "@" (keydown)
input.input.ki.wVk = new WinDef.WORD( '@' ); // 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 "@"
input.input.ki.wVk = new WinDef.WORD( '@' ); // 0x41
input.input.ki.dwFlags = new WinDef.DWORD( 2 ); // keyup
User32.INSTANCE.SendInput(
new WinDef.DWORD( 1 ),
( WinUser.INPUT[] ) input.toArray( 1 ),
input.size()
);
// Press "x"
input.input.ki.wVk = new WinDef.WORD( 'X' ); // 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 "x"
input.input.ki.wVk = new WinDef.WORD( 'X' ); // 0x41
input.input.ki.dwFlags = new WinDef.DWORD( 2 ); // keyup
User32.INSTANCE.SendInput(
new WinDef.DWORD( 1 ),
( WinUser.INPUT[] ) input.toArray( 1 ),
input.size()
);
}
}
}
public static Map<HWND, String> getWindows() {
final Map<HWND, String> map = new HashMap<HWND, String>();
User32.INSTANCE.EnumWindows(
new WNDENUMPROC() {
@Override
public boolean callback(HWND hWnd, Pointer arg1) {
String wText = getWindowTitle(hWnd);
map.put(hWnd, wText);
return true;
}
},
null
);
return map;
}
/**
* @param hWnd
* @return Title of a given process HWND.
*/
public static String getWindowTitle(HWND hWnd) {
char[] buffer = new char[MAX_TITLE_LENGTH * 2];
User32.INSTANCE.GetWindowText(hWnd, buffer, MAX_TITLE_LENGTH);
return Native.toString(buffer);
}
}
该程序只是不向记事本发送“ @”字符,而向其发送“ x”字符。 我应该怎么做才能发送“ @”字符?