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

Revisions

View the latest version of libbirdfont/KeyBindings.vala.
Max value in utf-16
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.UP == i || 56 Key.RIGHT == i || 57 Key.DOWN == i || 58 Key.LEFT == i || 59 Key.PG_UP == i || 60 Key.PG_DOWN == i || 61 Key.ENTER == i || 62 Key.BACK_SPACE == i || 63 Key.SHIFT_LEFT == i || 64 Key.SHIFT_RIGHT == i || 65 Key.CTRL_LEFT == i || 66 Key.CTRL_RIGHT == i || 67 Key.ALT_LEFT == i || 68 Key.ALT_RIGHT == i || 69 Key.ALT_GR == i || 70 Key.LOGO_LEFT == i || 71 Key.LOGO_RIGHT == i || 72 Key.TAB == i || 73 Key.CAPS_LOCK == i || 74 Key.LOGO_RIGHT == i || 75 Key.ENTER == i || 76 Key.DEL == i; 77 } 78 79 /** Modifier flags */ 80 public static const uint NONE = 0; 81 public static const uint CTRL = 1 << 0; 82 public static const uint ALT = 1 << 1; 83 public static const uint SHIFT = 1 << 2; 84 public static const uint LOGO = 1 << 3; 85 86 public class KeyBindings { 87 88 static bool modifier_ctrl = false; 89 static bool modifier_alt = false; 90 static bool modifier_shift = false; 91 static bool modifier_logo = false; 92 93 public static uint modifier = 0; 94 95 public static bool require_modifier; 96 97 public static uint get_modifiers () { 98 return modifier; 99 } 100 101 public static void reset () { 102 modifier = NONE; 103 modifier_ctrl = false; 104 modifier_alt = false; 105 modifier_shift = false; 106 modifier_logo = false; 107 } 108 109 public static void set_require_modifier (bool t) { 110 require_modifier = t; 111 } 112 113 public static uint get_mod_from_key (uint keyval) { 114 uint mod = NONE; 115 mod |= (keyval == Key.CTRL_RIGHT || keyval == Key.CTRL_LEFT) ? CTRL : 0; 116 mod |= (keyval == Key.SHIFT_RIGHT || keyval == Key.SHIFT_LEFT) ? SHIFT : 0; 117 mod |= (keyval == Key.ALT_LEFT || keyval == Key.ALT_GR) ? ALT : 0; 118 mod |= (keyval == Key.LOGO_LEFT || keyval == Key.LOGO_RIGHT) ? LOGO : 0; 119 return mod; 120 } 121 122 public static void remove_modifier_from_keyval (uint keyval) { 123 uint mod = 0xFFFFFF ^ get_mod_from_key (keyval); 124 set_modifier (modifier & mod); 125 } 126 127 public static void add_modifier_from_keyval (uint keyval) { 128 uint mod = get_mod_from_key (keyval); 129 set_modifier (modifier | mod); 130 } 131 132 public static void set_modifier (uint mod) { 133 modifier = mod; 134 135 modifier_ctrl = ((modifier & CTRL) > 0); 136 modifier_alt = ((modifier & ALT) > 0); 137 modifier_shift = ((modifier & SHIFT) > 0); 138 modifier_logo = ((modifier & LOGO) > 0); 139 } 140 141 public static bool has_alt () { 142 return modifier_alt; 143 } 144 145 public static bool has_shift () { 146 return modifier_shift; 147 } 148 149 public static bool has_ctrl () { 150 return modifier_ctrl; 151 } 152 153 public static bool has_logo () { 154 return modifier_logo; 155 } 156 } 157 158 } 159