2

I am creating a software to store passwords in order that they can be send to login windows of tokens. The problem is that some characters like "@" and "~" aren't send.

To show you the problem, I've changed the target window from the login window of the token to the window of the Notepad editor. If someone wants to run the code below, will see that the "x" character is sent to Notepad, but the "@" isn't.

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

The program just doesn't send the "@" character to Notepad, but in the other hand sends the "x" character to it. What should I do to be able to send the "@" character ?

Renato
  • 29
  • 3

0 Answers0