S
This can only be done with JNI, so the code will have to be written on C/C+++.I'm recommending that we look inside the library. https://github.com/kwhat/jnativehook , she's already been able to catch events.C++ code from Java can be called: (1) Create a Java-coded box:public class SendkeysTest {
static {
System.loadLibrary("sendkeys"); // sendkeys.dll (Windows) или sendkeys.so (Unix/GNU/Linux)
}
// Объявление native метода
private native void sendKeys();
// Проверяем, что все работает
public static void main(String[] args) {
new Sendkeys().sendKeys();
}
}
(2) write a programme on C++ (sendkeys_impl.cpp)#include "sendkeys.h"
#include <iostream>
using namespace std;
void sendKeys () {
//здесь код по отправке клавиш
return;
}
(3) Make a title for interaction with C code (sendkeys.h)#ifndef _SENDKEYS_H
#define _SENDKEYS_H
#ifdef __cplusplus
extern "C" {
#endif
void sendKeys();
#ifdef __cplusplus
}
#endif
#endif
(4) Formulate it as DLL, in C file (sendkeys.c) indicating such a design:#include <jni.h>
#include "sendkeys.h"
#include "sendkeys_impl.cpp"
JNIEXPORT void JNICALL Java_Sendkeys_sendKeys (JNIEnv *env, jobject thisObj) {
sendKeys(); // вызываем функцию, ранее написанную на C++
return;
}
(5) Collect somehow: g++ -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o sendkeys.dll sendkeys.c sendkeys.cpp(6) May be launched! java SendkeysTest or java -Djava.library.path=. SendkeysTestThis is described in greater detail https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html JNI .You can use any Tutorial to send a C++-clavict towers, or you can put that row on StackOverflow. The place where the code is inscribed is already in place.If you need to send random codes, you need to either. http://msdn.microsoft.com/en-us/library/ms646310%28VS.85%29.aspx (He's on windows 2k/xp and therefore preferable), or http://msdn.microsoft.com/en-us/library/ms646304%28VS.85%29.aspx (works in new DSs). There are other things. WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR events for SendMessage.For example, it is possible to send messages to the Notepad using the above mentioned information. SendInput:int SendKeystrokesToNotepad( const TCHAR *const text )
{
INPUT *keystroke;
UINT i, character_count, keystrokes_to_send, keystrokes_sent;
HWND notepad;
assert( text != NULL );
//Получаем handle до окна Notepad.
notepad = FindWindow( _T( "Notepad" ), NULL );
if( notepad == NULL )
return 0;
//Перетаскиваем окно вперед
if( !SetForegroundWindow( notepad ) )
return 0;
//Заполняем массив клавиш, которые будем посылать
character_count = _tcslen( text );
keystrokes_to_send = character_count * 2;
keystroke = new INPUT[ keystrokes_to_send ];
for( i = 0; i < character_count; ++i )
{
keystroke[ i * 2 ].type = INPUT_KEYBOARD;
keystroke[ i * 2 ].ki.wVk = 0;
keystroke[ i * 2 ].ki.wScan = text[ i ];
keystroke[ i * 2 ].ki.dwFlags = KEYEVENTF_UNICODE;
keystroke[ i * 2 ].ki.time = 0;
keystroke[ i * 2 ].ki.dwExtraInfo = GetMessageExtraInfo();
keystroke[ i * 2 + 1 ].type = INPUT_KEYBOARD;
keystroke[ i * 2 + 1 ].ki.wVk = 0;
keystroke[ i * 2 + 1 ].ki.wScan = text[ i ];
keystroke[ i * 2 + 1 ].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
keystroke[ i * 2 + 1 ].ki.time = 0;
keystroke[ i * 2 + 1 ].ki.dwExtraInfo = GetMessageExtraInfo();
}
//Посылаем клавиши
keystrokes_sent = SendInput( ( UINT )keystrokes_to_send, keystroke, sizeof( *keystroke ) );
delete [] keystroke;
return keystrokes_sent == keystrokes_to_send;
}
Or help. SendMessage:int SendKeystrokesToNotepad( const TCHAR *const text )
{
HWND notepad, edit;
assert( text != NULL );
//Берем handle до окна Notepad
notepad = FindWindow( _T( "Notepad" ), NULL );
if( notepad == NULL )
return 0;
//Берем handle до контрола edit на этом окне
edit = FindWindowEx( notepad, NULL, _T( "Edit" ), NULL );
if( edit == NULL )
return 0;
//Собственно, посылаем
SendMessage( edit, EM_REPLACESEL, ( WPARAM )TRUE, ( LPARAM )text );
return 1;
}
(Measures taken from https://stackoverflow.com/questions/2113950/how-to-send-keystrokes-to-a-window Main StackOverflow)The button of mice and its movement can also be sent through SendInput:INPUT Input={0};
Input.type = INPUT_MOUSE;
Input.mi.dx = (LONG)nx;
Input.mi.dy = (LONG)ny;
Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
SendInput(1,&Input,sizeof(INPUT));
Now, your job is to glue and adapt it to your task, to write a perfectly correct stitch on the Java+C+++ combination doesn't fit into a reasonable response.