android_keys_utils.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**************************************************************************/
  2. /* android_keys_utils.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "android_keys_utils.h"
  31. unsigned int godot_code_from_android_code(unsigned int p_code) {
  32. for (int i = 0; android_godot_code_pairs[i].android_code != AKEYCODE_MAX; i++) {
  33. if (android_godot_code_pairs[i].android_code == p_code) {
  34. return android_godot_code_pairs[i].godot_code;
  35. }
  36. }
  37. return KEY_UNKNOWN;
  38. }
  39. unsigned int godot_code_from_unicode(unsigned int p_code) {
  40. unsigned int code = p_code;
  41. if (code > 0xFF) {
  42. return KEY_UNKNOWN;
  43. }
  44. // Known control codes.
  45. if (code == '\b') { // 0x08
  46. return KEY_BACKSPACE;
  47. }
  48. if (code == '\t') { // 0x09
  49. return KEY_TAB;
  50. }
  51. if (code == '\n') { // 0x0A
  52. return KEY_ENTER;
  53. }
  54. if (code == 0x1B) {
  55. return KEY_ESCAPE;
  56. }
  57. if (code == 0x7F) {
  58. return KEY_DELETE;
  59. }
  60. // Unknown control codes.
  61. if (code <= 0x1F || (code >= 0x80 && code <= 0x9F)) {
  62. return KEY_UNKNOWN;
  63. }
  64. // Convert to uppercase.
  65. if (code >= 'a' && code <= 'z') { // 0x61 - 0x7A
  66. code -= ('a' - 'A');
  67. }
  68. if (code >= u'à' && code <= u'ö') { // 0xE0 - 0xF6
  69. code -= (u'à' - u'À'); // 0xE0 - 0xC0
  70. }
  71. if (code >= u'ø' && code <= u'þ') { // 0xF8 - 0xFF
  72. code -= (u'ø' - u'Ø'); // 0xF8 - 0xD8
  73. }
  74. return code;
  75. }