2
using System.Collections.Generic;
8
using Do.Platform.Common;
10
namespace Do.Platform.Common
12
public abstract class AbstractKeyBindingService : IKeyBindingService
15
public abstract bool RegisterOSKey (string keyString, EventCallback cb);
16
public abstract bool UnRegisterOSKey (string keyString);
20
#region IInitializedService
22
public void Initialize ()
24
Bindings = new List<KeyBinding> ();
25
prefs = Services.Preferences.Get<AbstractKeyBindingService> ();
30
#region IKeyBindingService
32
public List<KeyBinding> Bindings { get; private set; }
34
public bool RegisterKeyBinding (KeyBinding binding)
36
//try to get the keystring from the prefs. We default to the KeyBinding.KeyString, so we can later check
37
//if the prefs value matches that, we're using the default, otherwise we're using a user specified value
38
string prefsKeyString = prefs.Get (binding.PreferenceName, binding.KeyString);
40
//if these values don't match then the user has specified a new keystring
41
//update the KeyEvent then continue
42
if (prefsKeyString != binding.KeyString)
43
binding.KeyString = prefsKeyString;
45
//check if this keystring is already used
46
if (Bindings.Any (k => k.KeyString == binding.KeyString)) {
47
Log<AbstractKeyBindingService>.Error ("Key \"{0}\" is already mapped.", binding.KeyString);
48
binding.KeyString = "";
51
//if we are registering a key with the OS, do something special
52
if (binding.IsOSKey) {
53
//try to register the key from the prefs with the OS
54
if (!RegisterOSKey (binding.KeyString, binding.Callback)) {
55
//if we fail to register the summon key, try again with the default binding
56
if (RegisterOSKey (binding.DefaultKeyString, binding.Callback)) {
57
binding.KeyString = binding.DefaultKeyString;
59
Log<AbstractKeyBindingService>.Error ("Failed to bind \"{0}\" to \"{1}\"", binding.Description,
61
binding.KeyString = "";
66
//add the event to the list of bindings
67
Bindings.Add (binding);
68
//set the bound keystring in the prefs
69
prefs.Set (binding.Description.Replace (' ', '_'), binding.KeyString);
74
public bool SetKeyString (KeyBinding binding, string newKeyString)
76
//first check if this keystring exists
77
if (!Bindings.Any (k => k.KeyString == binding.KeyString))
80
//if this key should be registered with the OS
81
if (binding.IsOSKey) {
82
//remove the old keystring from the OS
83
UnRegisterOSKey (binding.KeyString);
84
//register again with the new keystring
85
RegisterOSKey (newKeyString, binding.Callback);
88
//set the new keystring
89
Bindings.First (k => k.Description == binding.Description).KeyString = newKeyString;
91
//save the new value in the prefs
92
prefs.Set (binding.Description.Replace (' ', '_'), binding.KeyString);
94
if (!string.IsNullOrEmpty (binding.KeyString))
95
Log<AbstractKeyBindingService>.Debug ("\"{0}\" now mapped to \"{1}\"", binding.Description, binding.KeyString);
101
/// Converts a keypress into a human readable string for comparing
102
/// against values in GConf.
104
/// <param name="evnt">
105
/// A <see cref="EventKey"/>
108
/// A <see cref="System.String"/> in the form "<Modifier>key"
110
public string KeyEventToString (Gdk.EventKey evnt) {
111
string modifier = "";
112
if ((evnt.State & Gdk.ModifierType.ControlMask) != 0) {
113
modifier += "<Control>";
115
if ((evnt.State & Gdk.ModifierType.SuperMask) != 0) {
116
modifier += "<Super>";
118
if ((evnt.State & Gdk.ModifierType.Mod1Mask) != 0) {
121
if ((evnt.State & Gdk.ModifierType.ShiftMask) != 0) {
122
modifier += "<Shift>";
123
//if we're pressing shift, and the key is ISO_Left_Tab,
125
if (evnt.Key == Gdk.Key.ISO_Left_Tab)
126
return string.Format ("{0}{1}", modifier, Gdk.Key.Tab);
128
return string.Format ("{0}{1}", modifier, Gtk.Accelerator.Name (evnt.KeyValue, Gdk.ModifierType.None));
2
using System.Collections.Generic;
8
namespace Do.Platform.Common
10
public abstract class AbstractKeyBindingService : IKeyBindingService
13
public abstract bool RegisterOSKey (string keyString, EventCallback cb);
14
public abstract bool UnRegisterOSKey (string keyString);
18
#region IInitializedService
20
public void Initialize ()
22
Bindings = new List<KeyBinding> ();
23
prefs = Services.Preferences.Get<AbstractKeyBindingService> ();
28
#region IKeyBindingService
30
public List<KeyBinding> Bindings { get; private set; }
32
public bool RegisterKeyBinding (KeyBinding binding)
34
//try to get the keystring from the prefs. We default to the KeyBinding.KeyString, so we can later check
35
//if the prefs value matches that, we're using the default, otherwise we're using a user specified value
36
string prefsKeyString = prefs.Get (binding.PreferenceName, binding.KeyString);
38
//if these values don't match then the user has specified a new keystring
39
//update the KeyEvent then continue
40
if (prefsKeyString != binding.KeyString)
41
binding.KeyString = prefsKeyString;
43
//check if this keystring is already used
44
if (Bindings.Any (k => k.KeyString == binding.KeyString)) {
45
Log<AbstractKeyBindingService>.Error ("Key \"{0}\" is already mapped.", binding.KeyString);
46
binding.KeyString = "";
49
//if we are registering a key with the OS, do something special
50
if (binding.IsOSKey) {
51
//try to register the key from the prefs with the OS
52
if (!RegisterOSKey (binding.KeyString, binding.Callback)) {
53
//if we fail to register the summon key, try again with the default binding
54
if (!string.IsNullOrEmpty (binding.DefaultKeyString) && RegisterOSKey (binding.DefaultKeyString, binding.Callback)) {
55
binding.KeyString = binding.DefaultKeyString;
56
} else if (!string.IsNullOrEmpty (binding.KeyString) && !string.IsNullOrEmpty (binding.DefaultKeyString)) {
57
Log<AbstractKeyBindingService>.Error ("Failed to bind \"{0}\" to \"{1}\"", binding.Description,
59
binding.KeyString = "";
64
//add the event to the list of bindings
65
Bindings.Add (binding);
66
//set the bound keystring in the prefs
67
prefs.Set (binding.PreferenceName, binding.KeyString);
72
public bool SetKeyString (KeyBinding binding, string newKeyString)
74
//first check if this keystring exists
75
if (!Bindings.Any (k => k.KeyString == binding.KeyString))
78
//if this key should be registered with the OS
79
if (binding.IsOSKey) {
80
//remove the old keystring from the OS
81
UnRegisterOSKey (binding.KeyString);
82
//register again with the new keystring
83
RegisterOSKey (newKeyString, binding.Callback);
86
//set the new keystring
87
Bindings.First (k => k.Description == binding.Description).KeyString = newKeyString;
89
//save the new value in the prefs
90
prefs.Set (binding.PreferenceName, binding.KeyString);
92
if (!string.IsNullOrEmpty (binding.KeyString))
93
Log<AbstractKeyBindingService>.Debug ("\"{0}\" now mapped to \"{1}\"", binding.Description, binding.KeyString);
99
/// Converts a keypress into a human readable string for comparing
100
/// against values in GConf.
102
/// <param name="evnt">
103
/// A <see cref="EventKey"/>
106
/// A <see cref="System.String"/> in the form "<Modifier>key"
108
public string KeyEventToString (uint keycode, uint modifierCode) {
109
// FIXME: This should really use Gtk.Accelerator.Name (key, modifier)
110
// Beware of bug #903566 when doing that!
112
string modifier = "";
113
if ((modifierCode & (uint)Gdk.ModifierType.ControlMask) != 0) {
114
modifier += "<Control>";
116
if ((modifierCode & (uint)Gdk.ModifierType.SuperMask) != 0) {
117
modifier += "<Super>";
119
if ((modifierCode & (uint)Gdk.ModifierType.Mod1Mask) != 0) {
122
if ((modifierCode & (uint)Gdk.ModifierType.ShiftMask) != 0) {
123
modifier += "<Shift>";
124
//if we're pressing shift, and the key is ISO_Left_Tab,
126
if (keycode == (uint)Gdk.Key.ISO_Left_Tab)
127
return string.Format ("{0}{1}", modifier, Gdk.Key.Tab);
129
return string.Format ("{0}{1}", modifier, Gtk.Accelerator.Name (keycode, Gdk.ModifierType.None));