本文介绍了你可以把NumLock键在XNA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以把NumLock键在XNA?

Can you turn NumLock on in XNA?

(我正在寻找一个解决方案,以 XNA数字锁定影响输入。 )

(I'm looking for a solution to XNA Number lock affects input.)

推荐答案

您得的P / Invoke的。这在某种程度上参与:

You'll have to P/Invoke SendInput. This is somewhat involved:

void ToggleNumLock() {
    var inputSequence = new INPUT[2]; // one keydown, one keyup = one keypress
    inputSequence[0].type = 1; // type Keyboard
    inputSequence[1].type = 1;
    inputSequence[0].U.wVk = 0x90; // keycode for NumLock
    inputSequence[1].U.wVk = 0x90;
    inputSequence[1].U.dwFlags |= KEYEVENTF.KEYUP;
    var rv = SendInput(2, inputSequence, INPUT.Size);
    if (rv != 2)
    {
        throw new InvalidOperationException("Call to SendInput failed");
    }
}

void EnsureNumLockIsOn() {
   bool numLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
   if (!numLock) {
       ToggleNumLock();
   }
}

下面是相关定义:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern uint SendInput(UInt32 nInputs, 
    INPUT[] pInputs,
    int cbSize);

[DllImport("user32.dll")]
static extern short GetKeyState(int keyCode);

[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
  internal uint type;
  internal KEYBDINPUT U;
  internal static int Size
  {
   get { return Marshal.SizeOf(typeof(INPUT)); }
  }
}

[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT
{
    internal short wVk;
    internal short wScan;
    internal KEYEVENTF dwFlags;
    internal int time;
    internal UIntPtr dwExtraInfo;
    uint unused1;
    uint unused2;
}

[Flags]
internal enum KEYEVENTF : uint
{
    EXTENDEDKEY = 0x0001,
    KEYUP = 0x0002,
    SCANCODE = 0x0008,
    UNICODE = 0x0004
}

这篇关于你可以把NumLock键在XNA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 11:33