@ -2,7 +2,6 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.Text ;
using Avalonia.Input ;
using Avalonia.Input ;
using Avalonia.Win32.Interop ;
using Avalonia.Win32.Interop ;
@ -211,7 +210,7 @@ namespace Avalonia.Win32.Input
{ 3 1 , Key . ImeModeChange } ,
{ 3 1 , Key . ImeModeChange } ,
{ 3 2 , Key . Space } ,
{ 3 2 , Key . Space } ,
{ 3 3 , Key . PageUp } ,
{ 3 3 , Key . PageUp } ,
{ 3 4 , Key . Next } ,
{ 3 4 , Key . PageDown } ,
{ 3 5 , Key . End } ,
{ 3 5 , Key . End } ,
{ 3 6 , Key . Home } ,
{ 3 6 , Key . Home } ,
{ 3 7 , Key . Left } ,
{ 3 7 , Key . Left } ,
@ -364,17 +363,80 @@ namespace Avalonia.Win32.Input
{ 2 5 4 , Key . OemClear } ,
{ 2 5 4 , Key . OemClear } ,
} ;
} ;
public static Key KeyFromVirtualKey ( int virtualKey )
/// <summary>
/// Indicates whether the key is an extended key, such as the right-hand ALT and CTRL keys.
/// According to https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-keydown.
/// </summary>
private static bool IsExtended ( int keyData )
{
{
Key result ;
const int extendedMask = 1 < < 2 4 ;
s_keyFromVirtualKey . TryGetValue ( virtualKey , out result ) ;
return ( keyData & extendedMask ) ! = 0 ;
}
private static int GetVirtualKey ( int virtualKey , int keyData )
{
// Adapted from https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndKeyboardInputProvider.cs.
if ( virtualKey = = ( int ) UnmanagedMethods . VirtualKeyStates . VK_SHIFT )
{
// Bits from 16 to 23 represent scan code.
const int scanCodeMask = 0xFF0000 ;
var scanCode = ( keyData & scanCodeMask ) > > 1 6 ;
virtualKey = ( int ) UnmanagedMethods . MapVirtualKey ( ( uint ) scanCode , ( uint ) UnmanagedMethods . MapVirtualKeyMapTypes . MAPVK_VSC_TO_VK_EX ) ;
if ( virtualKey = = 0 )
{
virtualKey = ( int ) UnmanagedMethods . VirtualKeyStates . VK_LSHIFT ;
}
}
if ( virtualKey = = ( int ) UnmanagedMethods . VirtualKeyStates . VK_MENU )
{
bool isRight = IsExtended ( keyData ) ;
if ( isRight )
{
virtualKey = ( int ) UnmanagedMethods . VirtualKeyStates . VK_RMENU ;
}
else
{
virtualKey = ( int ) UnmanagedMethods . VirtualKeyStates . VK_LMENU ;
}
}
if ( virtualKey = = ( int ) UnmanagedMethods . VirtualKeyStates . VK_CONTROL )
{
bool isRight = IsExtended ( keyData ) ;
if ( isRight )
{
virtualKey = ( int ) UnmanagedMethods . VirtualKeyStates . VK_RCONTROL ;
}
else
{
virtualKey = ( int ) UnmanagedMethods . VirtualKeyStates . VK_LCONTROL ;
}
}
return virtualKey ;
}
public static Key KeyFromVirtualKey ( int virtualKey , int keyData )
{
virtualKey = GetVirtualKey ( virtualKey , keyData ) ;
s_keyFromVirtualKey . TryGetValue ( virtualKey , out var result ) ;
return result ;
return result ;
}
}
public static int VirtualKeyFromKey ( Key key )
public static int VirtualKeyFromKey ( Key key )
{
{
int result ;
s_virtualKeyFromKey . TryGetValue ( key , out var result ) ;
s_virtualKeyFromKey . TryGetValue ( key , out result ) ;
return result ;
return result ;
}
}
}
}