1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
14 * @author Toni Kalajainen
\r
16 package org.simantics.g2d.participant;
\r
18 import java.util.HashSet;
\r
19 import java.util.Set;
\r
21 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
\r
22 import org.simantics.scenegraph.g2d.events.KeyEvent;
\r
23 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
\r
24 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
\r
25 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;
\r
29 * KeyStatus monitors statuses of keys.
\r
30 * @see java.awt.event.KeyEvent
\r
32 public class KeyUtil extends AbstractCanvasParticipant {
\r
35 private Set<Integer> pressedByKeyCode = new HashSet<Integer>();
\r
36 private Set<Character> pressedByChar = new HashSet<Character>();
\r
38 @EventHandler(priority = Integer.MAX_VALUE)
\r
39 public boolean handleClick(KeyEvent e) {
\r
40 if (e instanceof KeyPressedEvent) {
\r
41 pressedByKeyCode.add(e.keyCode);
\r
42 pressedByChar.add(e.character);
\r
44 if (e instanceof KeyReleasedEvent) {
\r
45 pressedByKeyCode.remove(e.keyCode);
\r
46 pressedByChar.remove(e.character);
\r
51 public boolean isCharPressed(char c)
\r
53 return pressedByChar.contains(c);
\r
57 * See awt key event
\r
58 * @see java.awt.event.KeyEvent
\r
62 public boolean isKeyPressed(int keyCode)
\r
64 return pressedByKeyCode.contains(keyCode);
\r