Forums


Comfort Software :: Forums :: Development
<< Sujet précédent | Sujet suivant >>   

How to control the On-screen Keyboard from java

Auteur Message
Matthew Sandoz
Tue May 20 2008, 10:49PM

Utilisateur enregistré #114
Inscrit le: Tue May 20 2008, 10:06PM
Messages: 1
i have adapted the microsoft code in the help file to work from java. so far i have only tested this on vista but i think it should work on any windows box. it requires jawin dll and jar available at their sourceforge site - a link is in the code.

/*
 * This file is heavily based on Jawin: <a href="http://jawinproject.sourceforge.net/" rel="external">http://jawinproject.sourceforge.net/</a>
 *
 * assumes ComfortSoftware keyboard is loaded...
 * 
 */

package client.keyboard;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.jawin.COMException;
import org.jawin.FuncPtr;
import org.jawin.ReturnFlags;
import org.jawin.io.LittleEndianInputStream;
import org.jawin.io.LittleEndianOutputStream;
import org.jawin.io.NakedByteStream;

public class ComfortSoftwareKeyboard {

    protected static final String COMFORT_SOFTWARE_WINDOW_NAME = "CKeyboardFirstForm";
    protected static final String COMFORT_SOFTWARE_CLASS_NAME = "TFirstForm";
    
    protected static final int WM_USER = 1024;
    protected static final int WM_CSKEYBOARD = WM_USER + 192;
    protected static final int WM_CSKEYBOARDMOVE = WM_USER + 193;

    protected static final Call FIND_WINDOW = new Call("USER32.DLL", "FindWindowW", "GG:I:", 8);
    protected static final Call POST_MESSAGE = new Call("USER32.DLL", "PostMessageW", "IIII:I:", 16);
    
    private static ComfortSoftwareKeyboard INSTANCE = new ComfortSoftwareKeyboard();

    public static ComfortSoftwareKeyboard getInstance() {
        return INSTANCE;
    }

    protected int getWindowHandle() throws COMException, IOException {
        FuncPtr findWindow = null;
        findWindow = new FuncPtr(FIND_WINDOW.getDllName(), FIND_WINDOW.getFunctionName());
        NakedByteStream bs = new NakedByteStream();
        LittleEndianOutputStream leo = new LittleEndianOutputStream(bs);
        leo.writeStringUnicode(COMFORT_SOFTWARE_CLASS_NAME);
        leo.writeStringUnicode(COMFORT_SOFTWARE_WINDOW_NAME);
        byte[] b = findWindow.invoke(FIND_WINDOW.getParameterDescription(), FIND_WINDOW.getStackSize(), bs, null,
                ReturnFlags.CHECK_FALSE);
        LittleEndianInputStream leis = new LittleEndianInputStream(
                new ByteArrayInputStream(b));
        int l = leis.readInt();
        findWindow.close();
        return l;
    }

    public int move(int x, int y) throws COMException, IOException {
        int hWnd = getWindowHandle();

        FuncPtr postMessage = null;
        postMessage = new FuncPtr(POST_MESSAGE.getDllName(), POST_MESSAGE.getFunctionName());
        NakedByteStream bs = new NakedByteStream();
        LittleEndianOutputStream leo = new LittleEndianOutputStream(bs);

        leo.writeInt(hWnd);
        leo.writeInt(WM_CSKEYBOARDMOVE);
        leo.writeInt(x);
        leo.writeInt(y);

        byte[] b = postMessage.invoke(POST_MESSAGE.getParameterDescription(), POST_MESSAGE.getStackSize(), bs, null,
                ReturnFlags.CHECK_FALSE);
        LittleEndianInputStream leis = new LittleEndianInputStream(
                new ByteArrayInputStream(b));
        int l = leis.readInt();
        postMessage.close();
        return l;
    }
    
    public int setVisible(boolean visible) throws COMException, IOException {
        int hWnd = getWindowHandle();

        FuncPtr postMessage = null;
        postMessage = new FuncPtr(POST_MESSAGE.getDllName(), POST_MESSAGE.getFunctionName());
        NakedByteStream bs = new NakedByteStream();
        LittleEndianOutputStream leo = new LittleEndianOutputStream(bs);

        leo.writeInt(hWnd);
        leo.writeInt(WM_CSKEYBOARD);
        leo.writeInt(visible ? 1 : 2);
        leo.writeInt(0);

        byte[] b = postMessage.invoke(POST_MESSAGE.getParameterDescription(), POST_MESSAGE.getStackSize(), bs, null,
                ReturnFlags.CHECK_FALSE);
        LittleEndianInputStream leis = new LittleEndianInputStream(
                new ByteArrayInputStream(b));
        int l = leis.readInt();
        postMessage.close();
        return l;
    }

    public static void main(String[] args) throws Exception {
        try {
            ComfortSoftwareKeyboard keyboard = ComfortSoftwareKeyboard.getInstance();
            keyboard.setVisible(true);
            Thread.sleep(1000);
            keyboard.setVisible(false);
            Thread.sleep(1000);
            keyboard.setVisible(true);
            for (int i = 0; i < 100;i++) {
                keyboard.move(i, i);
            }
        } catch (COMException e) {
        } finally {
        }
    }
}

class Call {
    private int stackSize;
    private String functionName;
    private String parameterDescription;
    private String dllName;
    
    public Call(String dllName, String functionName, String parameterDescription, int stackSize) {
        this.stackSize = stackSize;
        this.functionName = functionName;
        this.parameterDescription = parameterDescription;
        this.dllName = dllName;
    }
    public int getStackSize() {return stackSize;}
    public String getFunctionName() {return functionName;}
    public String getParameterDescription() {return parameterDescription;}
    public String getDllName() {return dllName;}
}



[ Édité Tue May 20 2008, 11:34PM ]
Retour en haut
Comfort
Wed May 21 2008, 03:20PM


Inscrit le: Wed Sep 19 2007, 03:42PM
Messages: 617
Excellent! Thank you.

Have a nice day
Retour en haut
Modérateurs: Comfort

Allez à :     Retour en haut

Syndiquer ce sujet: rss 0.92 Syndiquer ce sujet: rss 2.0 Syndiquer ce sujet: RDF
Powered by e107 Forum System