~ubuntu-branches/ubuntu/utopic/sikuli/utopic

« back to all changes in this revision

Viewing changes to sikuli-script/src/main/java/org/sikuli/script/internal/hotkey/LinuxHotkeyManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Gilles Filippini
  • Date: 2011-10-04 23:32:13 UTC
  • mfrom: (4.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20111004233213-36fm78hx0z53tkuw
Tags: 1.0~x~rc3-dfsg1-2
* New patch fix-cmake-sikuli-ide.patch:
  + Fix random FTBFS due to missing inter target dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010-2011, Sikuli.org
 
3
 * Released under the MIT License.
 
4
 *
 
5
 */
 
6
package org.sikuli.script.internal.hotkey;
 
7
 
 
8
import java.lang.reflect.*;
 
9
import java.awt.Event;
 
10
import java.awt.event.*;
 
11
import java.util.*;
 
12
import java.io.IOException;
 
13
 
 
14
import org.sikuli.script.Debug;
 
15
import org.sikuli.script.HotkeyListener;
 
16
import org.sikuli.script.HotkeyEvent;
 
17
 
 
18
import jxgrabkey.HotkeyConflictException;
 
19
import jxgrabkey.JXGrabKey;
 
20
 
 
21
public class LinuxHotkeyManager extends HotkeyManager {
 
22
   static{
 
23
      try{
 
24
         System.loadLibrary("JXGrabKey");
 
25
      }
 
26
      catch(Exception e){
 
27
         Debug.error("Can't load native lib JXGrabKey");
 
28
         e.printStackTrace();
 
29
      }
 
30
   }
 
31
 
 
32
   class HotkeyData {
 
33
      int key, modifiers;
 
34
      HotkeyListener listener;
 
35
 
 
36
      public HotkeyData(int key_, int mod_, HotkeyListener l_){
 
37
         key = key_;
 
38
         modifiers = mod_;
 
39
         listener = l_;
 
40
      }
 
41
   };
 
42
 
 
43
   class MyHotkeyHandler implements jxgrabkey.HotkeyListener{
 
44
      public void onHotkey(int id){
 
45
         Debug.log(4, "Hotkey pressed");
 
46
         HotkeyData data = _idCallbackMap.get(id);
 
47
         HotkeyEvent e = new HotkeyEvent(data.key, data.modifiers);
 
48
         data.listener.invokeHotkeyPressed(e);
 
49
      }
 
50
   };
 
51
 
 
52
   private Map<Integer, HotkeyData> _idCallbackMap = new HashMap<Integer,HotkeyData >();
 
53
   private int _gHotkeyId = 1;
 
54
 
 
55
   public boolean _addHotkey(int keyCode, int modifiers, HotkeyListener listener){
 
56
      JXGrabKey grabKey = JXGrabKey.getInstance();
 
57
 
 
58
      if(_gHotkeyId == 1){
 
59
         grabKey.addHotkeyListener(new MyHotkeyHandler());
 
60
      }
 
61
 
 
62
      _removeHotkey(keyCode, modifiers);
 
63
      int id = _gHotkeyId++;
 
64
      HotkeyData data = new HotkeyData(keyCode, modifiers, listener);
 
65
      _idCallbackMap.put(id, data);
 
66
 
 
67
      try{
 
68
         //JXGrabKey.setDebugOutput(true);
 
69
         grabKey.registerAwtHotkey(id, modifiers, keyCode);
 
70
      }catch(HotkeyConflictException e){
 
71
         Debug.error("Hot key conflicts");
 
72
         return false;
 
73
      }
 
74
      return true;
 
75
   }
 
76
 
 
77
   public boolean _removeHotkey(int keyCode, int modifiers){
 
78
      for( Map.Entry<Integer, HotkeyData> entry : _idCallbackMap.entrySet() ){
 
79
         HotkeyData data = entry.getValue();
 
80
         if(data.key == keyCode && data.modifiers == modifiers){
 
81
            JXGrabKey grabKey = JXGrabKey.getInstance();
 
82
            int id = entry.getKey();
 
83
            grabKey.unregisterHotKey(id); 
 
84
            _idCallbackMap.remove(id);
 
85
            return true;
 
86
         }
 
87
      }
 
88
      return false;
 
89
   }
 
90
 
 
91
 
 
92
   public void cleanUp(){
 
93
      JXGrabKey grabKey = JXGrabKey.getInstance();
 
94
      for( Map.Entry<Integer, HotkeyData> entry : _idCallbackMap.entrySet() ){
 
95
         int id = entry.getKey();
 
96
         grabKey.unregisterHotKey(id); 
 
97
      }
 
98
      _gHotkeyId = 1;
 
99
      _idCallbackMap.clear();
 
100
      grabKey.getInstance().cleanUp(); 
 
101
   }
 
102
 
 
103
}
 
104
 
 
105