macOS: Move get_unicode_from_key_code() to she/osx/vk.mm

This commit is contained in:
David Capello 2017-01-05 17:33:25 -03:00
parent eb0f046dc7
commit 0b954087a9
4 changed files with 86 additions and 61 deletions

View File

@ -1,5 +1,5 @@
// SHE library
// Copyright (C) 2015-2016 David Capello
// Copyright (C) 2015-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -13,13 +13,11 @@
#include "gfx/point.h"
#include "she/event.h"
#include "she/event_queue.h"
#include "she/keys.h"
#include "she/osx/generate_drop_files.h"
#include "she/osx/vk.h"
#include "she/osx/window.h"
#include "she/system.h"
#include <Carbon/Carbon.h> // For VK codes
namespace she {
bool osx_is_key_pressed(KeyScancode scancode);
@ -80,51 +78,6 @@ KeyModifiers get_modifiers_from_nsevent(NSEvent* event)
return (KeyModifiers)modifiers;
}
// Based on code from:
// http://stackoverflow.com/questions/22566665/how-to-capture-unicode-from-key-events-without-an-nstextview
// http://stackoverflow.com/questions/12547007/convert-key-code-into-key-equivalent-string
// http://stackoverflow.com/questions/8263618/convert-virtual-key-code-to-unicode-string
//
// It includes a "translateDeadKeys" flag to avoid processing dead
// keys in case that we want to use key
CFStringRef get_unicode_from_key_code(NSEvent* event,
const bool translateDeadKeys)
{
// The "TISCopyCurrentKeyboardInputSource()" doesn't contain
// kTISPropertyUnicodeKeyLayoutData (returns nullptr) when the input
// source is Japanese (Romaji/Hiragana/Katakana).
//TISInputSourceRef inputSource = TISCopyCurrentKeyboardInputSource();
TISInputSourceRef inputSource = TISCopyCurrentKeyboardLayoutInputSource();
CFDataRef keyLayoutData = (CFDataRef)TISGetInputSourceProperty(inputSource, kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout* keyLayout =
(keyLayoutData ? (const UCKeyboardLayout*)CFDataGetBytePtr(keyLayoutData): nullptr);
UInt32 deadKeyState = (translateDeadKeys ? g_lastDeadKeyState: 0);
UniChar output[4];
UniCharCount length;
// Reference here:
// https://developer.apple.com/reference/coreservices/1390584-uckeytranslate?language=objc
UCKeyTranslate(
keyLayout,
event.keyCode,
kUCKeyActionDown,
((event.modifierFlags >> 16) & 0xFF),
LMGetKbdType(),
(translateDeadKeys ? 0: kUCKeyTranslateNoDeadKeysMask),
&deadKeyState,
sizeof(output) / sizeof(output[0]),
&length,
output);
if (translateDeadKeys)
g_lastDeadKeyState = deadKeyState;
CFRelease(inputSource);
return CFStringCreateWithCharacters(kCFAllocatorDefault, output, length);
}
} // anonymous namespace
bool osx_is_key_pressed(KeyScancode scancode)
@ -223,7 +176,8 @@ using namespace she;
bool sendMsg = true;
CFStringRef strRef = get_unicode_from_key_code(event, false);
CFStringRef strRef = get_unicode_from_key_code(event.keyCode,
event.modifierFlags);
if (strRef) {
int length = CFStringGetLength(strRef);
if (length == 1)
@ -235,7 +189,9 @@ using namespace she;
g_pressedKeys[scancode] = (ev.unicodeChar() ? ev.unicodeChar(): 1);
if (g_translateDeadKeys) {
strRef = get_unicode_from_key_code(event, true);
strRef = get_unicode_from_key_code(event.keyCode,
event.modifierFlags,
&g_lastDeadKeyState);
if (strRef) {
int length = CFStringGetLength(strRef);
if (length > 0) {

24
src/she/osx/vk.h Normal file
View File

@ -0,0 +1,24 @@
// SHE library
// Copyright (C) 2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef SHE_OSX_VK_H_INCLUDED
#define SHE_OSX_VK_H_INCLUDED
#pragma once
#include "she/keys.h"
#include <Cocoa/Cocoa.h>
namespace she {
KeyScancode cocoavk_to_scancode(UInt16 keyCode);
CFStringRef get_unicode_from_key_code(const UInt16 keyCode,
const NSEventModifierFlags modifierFlags,
UInt32* deadKeyState = nullptr);
}
#endif

View File

@ -1,5 +1,5 @@
// SHE library
// Copyright (C) 2015 David Capello
// Copyright (C) 2015-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -8,13 +8,15 @@
#include "config.h"
#endif
#include "she/keys.h"
#include "she/osx/vk.h"
#include <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h> // TIS functions
namespace she {
KeyScancode cocoavk_to_scancode(UInt16 vk) {
KeyScancode cocoavk_to_scancode(UInt16 keyCode)
{
static KeyScancode keymap[256] = {
// 0x00
kKeyA, // 0x00 - kVK_ANSI_A
@ -153,9 +155,54 @@ KeyScancode cocoavk_to_scancode(UInt16 vk) {
kKeyUp, // 0x7E - kVK_UpArrow
kKeyNil // 0x7F - ?
};
if (vk < 0 || vk > 127)
vk = 0;
return keymap[vk];
if (keyCode < 0 || keyCode > 127)
keyCode = 0;
return keymap[keyCode];
}
} // namespace shse
// Based on code from:
// http://stackoverflow.com/questions/22566665/how-to-capture-unicode-from-key-events-without-an-nstextview
// http://stackoverflow.com/questions/12547007/convert-key-code-into-key-equivalent-string
// http://stackoverflow.com/questions/8263618/convert-virtual-key-code-to-unicode-string
//
// If "deadKeyState" is = nullptr, it doesn't process dead keys.
CFStringRef get_unicode_from_key_code(const UInt16 keyCode,
const NSEventModifierFlags modifierFlags,
UInt32* deadKeyState)
{
// The "TISCopyCurrentKeyboardInputSource()" doesn't contain
// kTISPropertyUnicodeKeyLayoutData (returns nullptr) when the input
// source is Japanese (Romaji/Hiragana/Katakana).
//TISInputSourceRef inputSource = TISCopyCurrentKeyboardInputSource();
TISInputSourceRef inputSource = TISCopyCurrentKeyboardLayoutInputSource();
CFDataRef keyLayoutData = (CFDataRef)TISGetInputSourceProperty(inputSource, kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout* keyLayout =
(keyLayoutData ? (const UCKeyboardLayout*)CFDataGetBytePtr(keyLayoutData): nullptr);
UInt32 deadKeyStateWrap = (deadKeyState ? *deadKeyState: 0);
UniChar output[4];
UniCharCount length;
// Reference here:
// https://developer.apple.com/reference/coreservices/1390584-uckeytranslate?language=objc
UCKeyTranslate(
keyLayout,
keyCode,
kUCKeyActionDown,
((modifierFlags >> 16) & 0xFF),
LMGetKbdType(),
(deadKeyState ? 0: kUCKeyTranslateNoDeadKeysMask),
&deadKeyStateWrap,
sizeof(output) / sizeof(output[0]),
&length,
output);
if (deadKeyState)
*deadKeyState = deadKeyStateWrap;
CFRelease(inputSource);
return CFStringCreateWithCharacters(kCFAllocatorDefault, output, length);
}
} // namespace she

View File

@ -1,5 +1,5 @@
// SHE library
// Copyright (C) 2012-2015 David Capello
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -18,8 +18,6 @@
namespace she {
class Surface;
KeyScancode cocoavk_to_scancode(UInt16 vk);
}
class OSXWindowImpl {