我有一个Microsoft响尾蛇操纵杆,我试图通过按钮点击事件出现在asp.net页面上。我添加了一个asp:timer来轮询操纵杆按钮,但按钮点击之间存在延迟,有时它们甚至没有出现。我试着把定时器延迟从1毫秒到2000毫秒不等,但似乎没什么用。
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<asp:ScriptManager runat="server" />
<asp:Timer runat="server" ID="pollTimer" OnTick="PollTimer" Enabled="True" Interval="1"/>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger runat="server" ControlID="pollTimer" EventName="Tick"/>
</Triggers>
<ContentTemplate>
<input id="ButtonXYZ" type="button" value="XYZ" runat="server" />
<table style="width: 30%;">
<tr>
<td style="width: 30px">
<input id="ButtonC" type="button" value="C" runat="server"/>
</td>
<td style="width: auto">
</td>
<td style="width: auto">
</td>
<td style="width: 70px" colspan="2" rowspan="2">
<input id="ButtonX" type="button" value="X" style="height: 63px; width: 70px;" runat="server" />
</td>
</tr>
<tr>
<td style="width: auto">
</td>
<td style="width: 30px">
<input id="ButtonB" type="button" value="B" runat="server" />
</td>
</tr>
<tr>
<td style="width: 30px">
<input id="ButtonD" type="button" value="D" runat="server"/>
</td>
<td style="width: auto">
</td>
<td style="width: 30px" rowspan="2">
<input id="ButtonUP" type="button" value="^" style="height: 63px;" runat="server" />
</td>
<td style="width: 30px" rowspan="2">
<input id="ButtonL" type="button" value="| |" style="height: 63px;" runat="server"/>
</td>
<td style="width: 30px">
<input id="ButtonO1" type="button" value="O" runat="server"/>
</td>
</tr>
<tr>
<td style="width: auto">
</td>
<td style="width: 30px">
<input id="ButtonA" type="button" value="A" runat="server"/>
</td>
<td style="width: 30px">
<input id="ButtonO2" type="button" value="O" runat="server"/>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Web.UI;
using Microsoft.DirectX.DirectInput;
using System.Windows.Forms;
using Timer = System.Web.UI.Timer;
namespace JoystickClientWebControl
{
public partial class _Default : System.Web.UI.Page
{
private Device _joystickDevice;
public DeviceCaps JoystickCapability { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
GetJoystick(sender, e);
}
private void GetJoystick(object sender, EventArgs e)
{
//--------------------------------------------------------------------------
// List of attached joysticks
var gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);
// There is one controller at least?
if ((gameControllerList.Count > 0))
{
// Select first joystick
gameControllerList.MoveNext();
// Create an object instance
if (gameControllerList.Current != null)
{
var deviceInstance = (DeviceInstance)gameControllerList.Current;
_joystickDevice = new Device(deviceInstance.InstanceGuid);
}
}
_joystickDevice.SetCooperativeLevel(null, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
_joystickDevice.SetDataFormat(DeviceDataFormat.Joystick);
_joystickDevice.Acquire();
JoystickCapability = _joystickDevice.Caps;
}
private void JoystickPolling()
{
try
{
_joystickDevice.Poll();
}
catch (Exception ex)
{
Debug.WriteLine(ex.StackTrace);
}
}
protected void PollTimer(object sender, EventArgs eventArgs)
{
_joystickDevice.Poll();
var state = _joystickDevice.CurrentJoystickState;
if (state.GetButtons()[0].Equals(128))
{
ButtonX.Style.Add(HtmlTextWriterStyle.BackgroundColor, "red");
Thread.Sleep(0);
}
else
{
ButtonX.Style.Clear();
Thread.Sleep(0);
}
}
}
}