My custom keyBoard problem

Hi all,

I want to develope a custom keyboard worked as MS screen keyboard.

It will used for machine who have not hard keyboard but own a screen toucher.

I add a MouseListener to every button of the keyboard, and transfer Robot.keypressed(VK_*) in the mousepressed method.

The problem is it seems that can only work for the component(JTextField, for example) on the app.

I mean it can work for any thing who will be edited.

How can I input text as MS screen keyboard: you press the caret in anywhere you will input, then move your mouse to the keyboard press the key you like.

thanks

[632 byte] By [thana] at [2007-11-27 6:55:11]
# 1
sorry, that is too platform specific for java. You will need some native library to give you low level access to the operating system like that.
gimbal2a at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 2
KeyEvent go to the component that has focus.You can use the requestFocusInWindow() method to reset focus. You can also use the Robot class to move the mouse around the screen.
camickra at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 3
What is native library?Could you give me some example.
thana at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 4

so, you have a screen, a mouse, but no keyboard.

this may be something like what you're trying to do (limited scale).

click into any of the textfields, then click a button to add the button's character

to the textfield.

import javax.swing.*;

import javax.swing.text.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

KeyboardButton[] keys = new KeyboardButton[3];

public void buildGUI()

{

JPanel keyboardPanel = new JPanel(new GridLayout(1,3));

for(int x = 0; x < keys.length; x++)

{

keys[x] = new KeyboardButton(""+(char)(x+65));

keyboardPanel.add(keys[x]);

}

JPanel textfieldPanel = new JPanel(new GridLayout(4,1));

for(int x = 0; x < 4; x++) textfieldPanel.add(new JTextField());

JFrame f = new JFrame();

f.getContentPane().add(keyboardPanel,BorderLayout.NORTH);

f.getContentPane().add(textfieldPanel,BorderLayout.CENTER);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

class KeyboardButton extends JButton

{

public KeyboardButton(final String letter)

{

super(letter);

setFocusable(false);//<--

setPreferredSize(new Dimension(50,50));

addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

Component comp = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

if(comp instanceof JTextComponent)

{

((JTextComponent)comp).setText(((JTextComponent)comp).getText()+letter);

}

}

});

}

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 5

You can let Robot implement the FocusListener interface and add the Robot instance to every interactive component in your application using addFocusListener().

(untested code example)public class Robot extends (...) implements ActionListener, FocusListener {

private Component focusedElement = null;

private int pressedMetaKeys = 0;

private int toggledMetaKeys = 0;

private JButton[] keys = new JButton[n]; // Typing keys

private JToggleButton[] metaKeys = new JToggleButton[6]; // Combinating keys (Ctrl, Alt, Shift, etc)

private JToggleButton[] toggleMetaKeys = new JToggleButton[6]; // Toggling combinating keys (Caps, Num, etc.)

(..)

public void focusGained(FocusEvent e) {

this.focusedElement = e.getComponent(); // try (Component)e.getSource() if this doesn't work

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() instanceof JToggleButton) {

(...) (update this.pressedMetaKeys or toggledMetaKeys using KeyEvent.SHIFT_DOWN_MASK, etc.)

}

else if (this.focusedElement != null) {

this.focusedElement.dispatchEvent(new KeyEvent((Component)e.getSource(), 0, System.currentTimeMillis(),

this.pressedMetaKeys^this.toggledMetaKeys, KeyEvent.VK_UNDEFINED, e.getActionCommand().getCharAt(0)));

this.releaseMetaKeys();

}

}

private void releaseMetaKeys() {

for(JToggleButton b : this.metaKeys) {

b.setEnabled(false); // Let the actionlistener clean up this.pressedMetaKeys

}

}

}

Message was edited by:

Zom-B

Zom-Ba at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 6
thanks Michael_Dunn,The problem is you can't input your "A,B,C" to something like notepad, word, or vi(Linux), etc.And this is the point.
thana at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 7
hi!which OS you are working on? and which make of the touch device you are using? which version of jdk is available there? lots of things are there to be known, to really provide any kind of solutionregardsAniruddha
Aniruddha-Herea at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 8

> The problem is you can't input your "A,B,C" to something like notepad, word, or vi(Linux), etc.

aha, I see what you're trying to do now.

OK, the code below is not a hack, it's just plain crap, but a bit of fun anyway.

open notepad, or word, run the program and have it so both windows are showing

i.e. word is not maximized etc.

click into notepad/word to give it focus (cursor blinking away), then click A B or C

like I said before, the code is just plain crap.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

JFrame f;

Robot robot;

KeyboardButton[] keys = new KeyboardButton[3];

public void buildGUI()

{

try{robot = new Robot();}catch(Exception e){}

JPanel keyboardPanel = new JPanel(new GridLayout(1,3));

for(int x = 0; x < keys.length; x++)

{

keys[x] = new KeyboardButton(""+(char)(x+65));

keyboardPanel.add(keys[x]);

}

f = new JFrame();

f.setFocusable(false);

f.getContentPane().add(keyboardPanel);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

class KeyboardButton extends JButton

{

public KeyboardButton(final String letter)

{

super(letter);

setPreferredSize(new Dimension(50,50));

addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

if(robot != null)

{

java.awt.datatransfer.StringSelection ss = new java.awt.datatransfer.StringSelection(letter);

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

f.setVisible(false);

robot.delay(150);//<--may need to increase, depending on pc speed

robot.keyPress(KeyEvent.VK_CONTROL);

robot.keyPress(KeyEvent.VK_V);

robot.keyRelease(KeyEvent.VK_V);

robot.keyRelease(KeyEvent.VK_CONTROL);

f.setVisible(true);

}

}

});

}

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...
# 9

Thanks all, the problem have been solved.

The api is running on CentOS5 with JDK1.5.

I have test it on Window XP professional, Window XP home and CentOS. It can work perfectly on XP pro and centos but cannnot on xp home.

I use Robot like Michael_Dunn does, but not use the style (copy to clipboardframe hidepaste to targetframe show). It seems strange with the frame blink.

thana at 2007-7-12 18:30:33 > top of Java-index,Desktop,Core GUI APIs...