swing - java.awt.event.KeyEvent not capable of fully mappin AZERTY keyboard? -
hello made java program display keyevent.getkeycode() each time press key.
the purpose able make kind of bot able drive git/skyrim/fraps using java robot class , keyevent.
however seems cannot map azerty keyboard, can see keyevent code same.
is there other way of driving keyboard?
here program if wondering :
main class :
package keykeykey; import java.awt.borderlayout; import javax.swing.jframe; import javax.swing.jtextfield; public class mainclass { public static void main(string args[]) throws exception { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); jtextfield nametextfield = new jtextfield(); frame.add(nametextfield, borderlayout.north); jtextfield codetextfield = new jtextfield(); frame.add(codetextfield, borderlayout.south); mykeylistener mykeylistener = new mykeylistener(codetextfield, nametextfield); nametextfield.addkeylistener(mykeylistener); frame.setsize(250, 100); frame.setvisible(true); } }
keylistener redefinition
package keykeykey; import java.awt.event.keyadapter; import java.awt.event.keyevent; import java.awt.event.keylistener; import javax.swing.jtextfield; public class mykeylistener extends keyadapter implements keylistener { public jtextfield codetextfield; public jtextfield writetextfield; public mykeylistener(jtextfield codetextfield, jtextfield writetextfield) { this.codetextfield = codetextfield; this.writetextfield = writetextfield; } @override public void keypressed(keyevent keyevent) { printit("pressed", keyevent); codetextfield.settext("code = " + keyevent.getkeycode()); } @override public void keyreleased(keyevent keyevent) { codetextfield.settext(""); writetextfield.settext(""); } @override public void keytyped(keyevent keyevent) { //do nothing } private void printit(string title, keyevent keyevent) { int keycode = keyevent.getkeycode(); string keytext = keyevent.getkeytext(keycode); system.out.println(title + " : " + keytext + " / " + keyevent.getkeychar() + " code = " + keyevent.getkeycode()); } }
thanks.
ps : yes know autohotkey, need typed language , love java.
edit
i must have not expressed myself correctly, know shift+vk1 makes "1" , same goes other keys.
my problem that, can see in first pictures, key generate unrecognized key event (the 1 red bars).
for example 2 keys :
a virtual key can changed based on modifiers applied to. actual result depended on keyboard driver itself, example...
shift+1 generate keyevent
keycode
keyevent.vk_1
, keychar
!
you ignoring these modifiers...
import java.awt.borderlayout; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.lang.reflect.field; import java.lang.reflect.modifier; import java.util.arraylist; import java.util.collections; import java.util.hashmap; import java.util.hashset; import java.util.list; import java.util.map; import java.util.set; import javax.swing.defaultlistmodel; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.keystroke; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testkeystrokes { public static void main(string[] args) { new testkeystrokes(); } public testkeystrokes() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private jlist keylist; private defaultlistmodel<string> model; public testpane() { setlayout(new borderlayout()); model = new defaultlistmodel<>(); keylist = new jlist(model); add(new jscrollpane(keylist)); keylist.addkeylistener(new keylistener() { @override public void keytyped(keyevent e) { } @override public void keypressed(keyevent e) { } @override public void keyreleased(keyevent e) { stringbuilder sb = new stringbuilder(64); if (e.isshiftdown()) { sb.append("[shift]"); } else if (e.isaltdown()) { sb.append("[alt]"); } else if (e.iscontroldown()) { sb.append("[ctrl]"); } else if (e.isaltgraphdown()) { sb.append("[alt-grph]"); } else if (e.ismetadown()) { sb.append("[meta]"); } switch (e.getkeycode()) { case keyevent.vk_shift: case keyevent.vk_alt: case keyevent.vk_control: case keyevent.vk_alt_graph: case keyevent.vk_meta: break; default: sb.append("[").append(getvktext(e.getkeycode())).append("/").append(e.getkeychar()).append("]"); break; } model.addelement(sb.tostring()); } }); } @override public dimension getpreferredsize() { return new dimension(200, 200); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); g2d.dispose(); } } private static vkcollection vkcollection; static vkcollection getvkcollection() { if (vkcollection == null) { vkcollection = new vkcollection(); } return vkcollection; } static string getvktext(int keycode) { vkcollection vkcollect = getvkcollection(); integer key = integer.valueof(keycode); string name = vkcollect.findname(key); if (name != null) { return name.substring(3); } int expected_modifiers = (modifier.public | modifier.static | modifier.final); field[] fields = keyevent.class.getdeclaredfields(); (int = 0; < fields.length; i++) { try { if (fields[i].getmodifiers() == expected_modifiers && fields[i].gettype() == integer.type && fields[i].getname().startswith("vk_") && fields[i].getint(keyevent.class) == keycode) { name = fields[i].getname(); vkcollect.put(name, key); return name.substring(3); } } catch (illegalaccessexception e) { assert (false); } } return "unknown"; } static class vkcollection { map code2name; map name2code; public vkcollection() { code2name = new hashmap(); name2code = new hashmap(); } public synchronized void put(string name, integer code) { assert ((name != null) && (code != null)); assert (findname(code) == null); assert (findcode(name) == null); code2name.put(code, name); name2code.put(name, code); } public synchronized integer findcode(string name) { assert (name != null); return (integer) name2code.get(name); } public synchronized string findname(integer code) { assert (code != null); return (string) code2name.get(code); } } }
Comments
Post a Comment