Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Public the ScanCode property in KeyEventArgs #8473

Open
lindexi opened this issue Nov 29, 2023 · 2 comments
Open

[API Proposal]: Public the ScanCode property in KeyEventArgs #8473

lindexi opened this issue Nov 29, 2023 · 2 comments
Labels
API suggestion Early API idea and discussion, it is NOT ready for implementation

Comments

@lindexi
Copy link
Contributor

lindexi commented Nov 29, 2023

Background and motivation

As unoplatform/uno#14371 (comment) , the UNO for WPF will use reflection to get the ScanCode property in KeyEventArgs.

And the ScanCode property in KeyEventArgs is useful in many businesses code or framework code.

This property is rarely used in WPF today.

The code to get the ScanCode (only ways) :

int scanCode = HwndKeyboardInputProvider.GetScanCode(msg.wParam, msg.lParam);

The code to use ScanCode (only ways) :

if (HandleAltNumpadEntry(keyArgs.RealKey, keyArgs.ScanCode, keyArgs.IsExtendedKey))

API Proposal

public class KeyEventArgs
{
    public int ScanCode { get; internal set; }
}

Or

public class KeyEventArgs
{
    public int ScanCode { get; set; }
}

API Usage

KeyEventArgs args = ...;
var scanCode = args.ScanCode.

Alternative Designs

No response

Risks

No response

@miloush
Copy link
Contributor

miloush commented Nov 29, 2023

Few things:

  1. Note that this is public on RawKeyboardInputReport. Also note that rather than reflection, people can use MapVirtualKey to get the SC from VK.
  2. This should be public get only like the other properties on KeyEventArgs.
  3. You need an information on the extended keys. A lazy option would be to make the IsExtendedKey public as well, but there is now MAPVK_VK_TO_VSC_EX which gives you the actual extended scancode. I would think that would be preferable.
  4. I am not entirely convinced this should be exposed on KeyEventArgs, because it doesn't even expose virtual keys. Maybe an interop method similar to getting VK would be more consistent.

@lindexi
Copy link
Contributor Author

lindexi commented Apr 30, 2024

Few things:

1. Note that this is public on `RawKeyboardInputReport`. Also note that rather than reflection, people can use MapVirtualKey to get the SC from VK.

2. This should be public get only like the other properties on `KeyEventArgs`.

3. You need an information on the extended keys. A lazy option would be to make the `IsExtendedKey` public as well, but there is now `MAPVK_VK_TO_VSC_EX` which gives you the actual extended scancode. I would think that would be preferable.

4. I am not entirely convinced this should be exposed on `KeyEventArgs`, because it doesn't even expose virtual keys. Maybe an interop method similar to getting VK would be more consistent.

For 1.

        var key = e.Key;
        var virtualKey = KeyInterop.VirtualKeyFromKey(key);

        // MAPVK_VK_TO_VSC 0
        var scanCode = MapVirtualKeyW((uint) virtualKey, 0);

        var scanCodeFromWpf = typeof(KeyEventArgs).GetProperty("ScanCode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(e);

        Debug.Assert(scanCode == (int) scanCodeFromWpf!);

    [DllImport("User32.dll")]
    private static extern uint MapVirtualKeyW(uint code, uint mapType);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API suggestion Early API idea and discussion, it is NOT ready for implementation
Projects
None yet
Development

No branches or pull requests

2 participants