Merge branch 'fix-keys'

This commit is contained in:
David Capello 2016-02-12 12:24:19 -03:00
commit 19f8aad2ef
2 changed files with 38 additions and 9 deletions

View File

@ -1,5 +1,5 @@
// SHE library
// Copyright (C) 2012-2015 David Capello
// Copyright (C) 2012-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -14,7 +14,8 @@
namespace she {
KeyScancode win32vk_to_scancode(int vk) {
KeyScancode win32vk_to_scancode(int vk)
{
static KeyScancode keymap[256] = {
// 0x00
kKeyNil, // 0x00
@ -34,9 +35,9 @@ KeyScancode win32vk_to_scancode(int vk) {
kKeyNil, // 0x0E - Undefined
kKeyNil, // 0x0F - Undefined
// 0x10
kKeyNil, // 0x10 - VK_SHIFT
kKeyNil, // 0x11 - VK_CONTROL
kKeyNil, // 0x12 - VK_MENU
kKeyLShift, // 0x10 - VK_SHIFT
kKeyLControl, // 0x11 - VK_CONTROL
kKeyAlt, // 0x12 - VK_MENU
kKeyPause, // 0x13 - VK_PAUSE
kKeyCapsLock, // 0x14 - VK_CAPITAL
kKeyKana, // 0x15 - VK_KANA
@ -294,7 +295,25 @@ KeyScancode win32vk_to_scancode(int vk) {
return keymap[vk];
}
static int scancode_to_win32vk(KeyScancode scancode) {
KeyModifiers get_modifiers_from_last_win32_message()
{
int modifiers = kKeyNoneModifier;
if ((GetKeyState(VK_LSHIFT) & 0x8000) ||
(GetKeyState(VK_RSHIFT) & 0x8000))
modifiers |= kKeyShiftModifier;
if ((GetKeyState(VK_LCONTROL) & 0x8000) ||
(GetKeyState(VK_RCONTROL) & 0x8000))
modifiers |= kKeyCtrlModifier;
if ((GetKeyState(VK_LMENU) & 0x8000) ||
(GetKeyState(VK_RMENU) & 0x8000))
modifiers |= kKeyAltModifier;
if (GetKeyState(VK_SPACE) & 0x8000)
modifiers |= kKeySpaceModifier;
return (KeyModifiers)modifiers;
}
static int scancode_to_win32vk(KeyScancode scancode)
{
static int initialized = false;
static int keymap[kKeyScancodes];
@ -319,7 +338,7 @@ bool is_key_pressed(KeyScancode scancode)
{
int vk = scancode_to_win32vk(scancode);
if (vk)
return (GetKeyState(vk) & 0xf000 ? true: false);
return (GetAsyncKeyState(vk) & 0x8000 ? true: false);
else
return false;
}

View File

@ -27,6 +27,7 @@
namespace she {
KeyScancode win32vk_to_scancode(int vk);
KeyModifiers get_modifiers_from_last_win32_message();
#define SHE_WND_CLASS_NAME L"Aseprite.Window"
@ -290,6 +291,7 @@ namespace she {
}
Event ev;
ev.setModifiers(get_modifiers_from_last_win32_message());
ev.setPosition(gfx::Point(
GET_X_LPARAM(lparam) / m_scale,
GET_Y_LPARAM(lparam) / m_scale));
@ -320,6 +322,7 @@ namespace she {
Event ev;
ev.setType(Event::MouseLeave);
ev.setModifiers(get_modifiers_from_last_win32_message());
queueEvent(ev);
}
break;
@ -329,6 +332,7 @@ namespace she {
case WM_MBUTTONDOWN: {
Event ev;
ev.setType(Event::MouseDown);
ev.setModifiers(get_modifiers_from_last_win32_message());
ev.setPosition(gfx::Point(
GET_X_LPARAM(lparam) / m_scale,
GET_Y_LPARAM(lparam) / m_scale));
@ -345,6 +349,7 @@ namespace she {
case WM_MBUTTONUP: {
Event ev;
ev.setType(Event::MouseUp);
ev.setModifiers(get_modifiers_from_last_win32_message());
ev.setPosition(gfx::Point(
GET_X_LPARAM(lparam) / m_scale,
GET_Y_LPARAM(lparam) / m_scale));
@ -366,6 +371,7 @@ namespace she {
case WM_RBUTTONDBLCLK: {
Event ev;
ev.setType(Event::MouseDoubleClick);
ev.setModifiers(get_modifiers_from_last_win32_message());
ev.setPosition(gfx::Point(
GET_X_LPARAM(lparam) / m_scale,
GET_Y_LPARAM(lparam) / m_scale));
@ -385,6 +391,7 @@ namespace she {
Event ev;
ev.setType(Event::MouseWheel);
ev.setModifiers(get_modifiers_from_last_win32_message());
ev.setPosition(gfx::Point(pos.x, pos.y) / m_scale);
int z = ((short)HIWORD(wparam)) / WHEEL_DELTA;
@ -407,6 +414,7 @@ namespace she {
Event ev;
ev.setType(Event::MouseWheel);
ev.setModifiers(get_modifiers_from_last_win32_message());
ev.setPosition(gfx::Point(pos.x, pos.y) / m_scale);
int bar = (msg == WM_HSCROLL ? SB_HORZ: SB_VERT);
@ -465,8 +473,9 @@ namespace she {
Event ev;
ev.setType(Event::KeyDown);
ev.setModifiers(get_modifiers_from_last_win32_message());
ev.setScancode(win32vk_to_scancode(vk));
ev.setRepeat(lparam & 0xffff);
ev.setRepeat(MAX(0, (lparam & 0xffff)-1));
if (charsInBuffer < 1) {
ev.setUnicodeChar(0);
@ -485,9 +494,10 @@ namespace she {
case WM_KEYUP: {
Event ev;
ev.setType(Event::KeyUp);
ev.setModifiers(get_modifiers_from_last_win32_message());
ev.setScancode(win32vk_to_scancode(wparam));
ev.setUnicodeChar(0);
ev.setRepeat(lparam & 0xffff);
ev.setRepeat(MAX(0, (lparam & 0xffff)-1));
queueEvent(ev);
// TODO If we use native menus, this message should be given