X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Fparticipant%2FKeyToCommand.java;fp=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Fparticipant%2FKeyToCommand.java;h=5093f1ec4abf6d157303deb54767d6e455ddaa80;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/KeyToCommand.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/KeyToCommand.java new file mode 100644 index 000000000..5093f1ec4 --- /dev/null +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/KeyToCommand.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g2d.participant; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant; +import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency; +import org.simantics.scenegraph.g2d.events.KeyEvent; +import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler; +import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent; +import org.simantics.scenegraph.g2d.events.command.CommandEvent; +import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding; +import org.simantics.scenegraph.g2d.events.command.Commands; + +/** + * Converts key presses to commands. + * Do not use this in eclipse environment, use XXXTODO instead which + * converts eclipse commands into g2d commands. + * + * CommandKeyBinding.DEFAULT_BINDINGS + * + * @See {@link CommandKeyBinding} command bindings + * @See {@link Commands} commands + * @author Toni Kalajainen + */ +public class KeyToCommand extends AbstractCanvasParticipant { + + @Dependency KeyUtil keyUtil; + List binds = new ArrayList(); + + public KeyToCommand(CommandKeyBinding... binds) { + assert(binds!=null); + for (CommandKeyBinding b : binds) + this.binds.add(b); + } + + public KeyToCommand(Collection binds) { + assert(binds!=null); + this.binds.addAll(binds); + } + + public void addBinding(CommandKeyBinding binding) { + if (!this.binds.contains(binding)) + this.binds.add(binding); + } + + @EventHandler(priority = Integer.MIN_VALUE) + public boolean handleKeyEvent(KeyEvent e) { + //System.out.println("keytocommand " + e); + boolean pressed = (e instanceof KeyPressedEvent); + nextBinding: + for (CommandKeyBinding ckb : binds) { + int keyCode = ckb.keyCode[0]; + boolean _down = keyCode>0; + if (_down != pressed) continue; + if (!_down) keyCode = -keyCode; + if (e.keyCode != keyCode) continue; + for (int i=1; i0; + if (!down) code = -code; + if (keyUtil.isKeyPressed(code) ^ down) + continue nextBinding; + } + CommandEvent ce = new CommandEvent(getContext(), e.time, ckb.command); + getContext().getEventQueue().queueFirst(ce); + } + return false; + } + +}