The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

KeyBindings.vala in /libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
Circle boundaries heads/master
1 /* 2 Copyright (C) 2012 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 namespace BirdFont { 16 17 public enum Key { 18 NONE = 0, 19 UP = 65362, 20 RIGHT = 65363, 21 DOWN = 65364, 22 LEFT = 65361, 23 PG_UP = 65365, 24 PG_DOWN = 65366, 25 ENTER = 65293, 26 BACK_SPACE = 65288, 27 SHIFT_LEFT = 65505, 28 SHIFT_RIGHT = 65506, 29 CTRL_LEFT = 65507, 30 CTRL_RIGHT = 65508, 31 CAPS_LOCK = 65509, 32 ALT_LEFT = 65513, 33 ALT_RIGHT = 65514, 34 ALT_GR = 65027, 35 LOGO_LEFT = 65515, 36 LOGO_RIGHT = 65516, 37 CONTEXT_MENU = 65383, 38 TAB = 65289, 39 SHIFT_TAB = 65056, 40 DEL = 65535, 41 NUM_PLUS = 65451, 42 NUM_MINUS = 65453, 43 END = 65367, 44 HOME = 65360 45 } 46 47 bool is_arrow_key (uint keyval) { 48 return keyval == Key.UP || 49 keyval == Key.DOWN || 50 keyval == Key.LEFT || 51 keyval == Key.RIGHT; 52 } 53 54 bool is_modifier_key (uint i) { 55 return Key.PG_UP == i || 56 Key.PG_DOWN == i || 57 Key.ENTER == i || 58 Key.BACK_SPACE == i || 59 Key.SHIFT_LEFT == i || 60 Key.SHIFT_RIGHT == i || 61 Key.CTRL_LEFT == i || 62 Key.CTRL_RIGHT == i || 63 Key.ALT_LEFT == i || 64 Key.ALT_RIGHT == i || 65 Key.ALT_GR == i || 66 Key.LOGO_LEFT == i || 67 Key.LOGO_RIGHT == i || 68 Key.TAB == i || 69 Key.CAPS_LOCK == i || 70 Key.LOGO_RIGHT == i || 71 Key.ENTER == i || 72 Key.DEL == i; 73 } 74 75 /** Modifier flags */ 76 public static const uint NONE = 0; 77 public static const uint CTRL = 1 << 0; 78 public static const uint ALT = 1 << 1; 79 public static const uint SHIFT = 1 << 2; 80 public static const uint LOGO = 1 << 3; 81 82 public class KeyBindings { 83 84 static bool modifier_ctrl = false; 85 static bool modifier_alt = false; 86 static bool modifier_shift = false; 87 static bool modifier_logo = false; 88 89 public static uint modifier = 0; 90 91 public static bool require_modifier; 92 93 public static uint get_modifiers () { 94 return modifier; 95 } 96 97 public static void reset () { 98 modifier = NONE; 99 modifier_ctrl = false; 100 modifier_alt = false; 101 modifier_shift = false; 102 modifier_logo = false; 103 } 104 105 public static void set_require_modifier (bool t) { 106 require_modifier = t; 107 } 108 109 public static uint get_mod_from_key (uint keyval) { 110 uint mod = NONE; 111 mod |= (keyval == Key.CTRL_RIGHT || keyval == Key.CTRL_LEFT) ? CTRL : 0; 112 mod |= (keyval == Key.SHIFT_RIGHT || keyval == Key.SHIFT_LEFT) ? SHIFT : 0; 113 mod |= (keyval == Key.ALT_LEFT || keyval == Key.ALT_GR) ? ALT : 0; 114 mod |= (keyval == Key.LOGO_LEFT || keyval == Key.LOGO_RIGHT) ? LOGO : 0; 115 return mod; 116 } 117 118 public static void remove_modifier_from_keyval (uint keyval) { 119 uint mod = 0xFFFFFF ^ get_mod_from_key (keyval); 120 set_modifier (modifier & mod); 121 } 122 123 public static void add_modifier_from_keyval (uint keyval) { 124 uint mod = get_mod_from_key (keyval); 125 set_modifier (modifier | mod); 126 } 127 128 public static void set_modifier (uint mod) { 129 modifier = mod; 130 131 modifier_ctrl = ((modifier & CTRL) > 0); 132 modifier_alt = ((modifier & ALT) > 0); 133 modifier_shift = ((modifier & SHIFT) > 0); 134 modifier_logo = ((modifier & LOGO) > 0); 135 } 136 137 public static bool has_alt () { 138 return modifier_alt; 139 } 140 141 public static bool has_shift () { 142 return modifier_shift; 143 } 144 145 public static bool has_ctrl () { 146 return modifier_ctrl; 147 } 148 149 public static bool has_logo () { 150 return modifier_logo; 151 } 152 } 153 154 } 155