我想知道当wpf不集中时如何从游戏手柄上获得输入。当专注时,它会起作用。 我正在创建一个程序,可以模拟Xbox控制器上的键盘输入和鼠标移动。我可以通过按箭头键(下面的代码)来移动鼠标。当我专注于该计划时,没有。 (稍后将更改为游戏手柄轴的移动。)
import {Component} from '@angular/core';
import {NgbDate, NgbCalendar} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'ngbd-datepicker-range',
templateUrl: './datepicker-range.html',
styles: [``]
})
export class NgbdDatepickerRange {
hoveredDate: NgbDate;
fromDate: NgbDate;
toDate: NgbDate;
constructor(calendar: NgbCalendar) {
this.fromDate = calendar.getToday();
this.toDate = calendar.getNext(calendar.getToday(), 'd', 10);
}
onDateSelection(date: NgbDate) {
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
} else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
this.toDate = date;
} else {
this.toDate = null;
this.fromDate = date;
}
}
isHovered = (date: NgbDate) => this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
isInside = (date: NgbDate) => date.after(this.fromDate) && date.before(this.toDate);
isRange = (date: NgbDate) => date.equals(this.fromDate) || date.equals(this.toDate) || this.isInside(date) || this.isHovered(date)
}
我的控制器管理器中的代码段
private void Init()
{
Thread th = new Thread(Update);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
public void SetPosition(int a, int b)
{
SetCursorPos(a, b);
}
public System.Drawing.Point GetMousePosition()
{
System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
return new System.Drawing.Point(point.X, point.Y);
}
void Update()
{
while (isRunning)
{
Thread.Sleep(40);
controllerManager.UpdateInput();
if ((Keyboard.GetKeyStates(Key.Up) & KeyStates.Down) > 0)
SetPosition(GetMousePosition().X, GetMousePosition().Y - speed);
if ((Keyboard.GetKeyStates(Key.Down) & KeyStates.Down) > 0)
SetPosition(GetMousePosition().X, GetMousePosition().Y + speed);
if ((Keyboard.GetKeyStates(Key.Right) & KeyStates.Down) > 0)
SetPosition(GetMousePosition().X + speed, GetMousePosition().Y);
if ((Keyboard.GetKeyStates(Key.Left) & KeyStates.Down) > 0)
SetPosition(GetMousePosition().X - speed, GetMousePosition().Y);
}
}
现在,当我没有聚焦时,我从Controller.GetCurrentReading()中什么也没得到。当程序在后台运行时,如果我没有焦点,如何获取输入。