задержка событий кнопки джойстика на asp.net - PullRequest
0 голосов
/ 12 февраля 2012

У меня есть джойстик Microsoft Sidewinder, и я пытаюсь пропустить события нажатия кнопки, чтобы появиться на странице asp.net.Я добавил таймер asp: для опроса кнопок джойстика, но между нажатиями кнопок существует задержка, и иногда они даже не появляются.Я попытался возиться с задержкой таймера в диапазоне от 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">
            &nbsp;
        </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">
            &nbsp;
        </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);
}
}
}
}
...