1

我正在构建一个 NFS 操纵杆来操纵 MouseWheel 事件。在我的主线程上,我调用了第二个线程,它以生成的频率脉冲键盘按键。我的问题是,当您向上或向下移动 MouseWheel 时,该频率会随时间动态变化,但模拟器线程不会注意到这种变化。这是我的代码片段:

int centro = 0;
static bool left;
static bool ismoving;
static int grado;
static int frec;

Thread simulator = new Thread(new ThreadStart(move));
public Form1()
{
    InitializeComponent();
}
static void mleft()
{
    KeyboardSimulator.KeyDown(Keys.G);
    Thread.Sleep(10);
    KeyboardSimulator.KeyUp(Keys.G);
}
static void mright()
{
    KeyboardSimulator.KeyDown(Keys.J);
    Thread.Sleep(10);
    KeyboardSimulator.KeyUp(Keys.J);
}
static void move()
{
    while (ismoving)
    {
        if (left)
        {
            mleft();
            Thread.Sleep(frec);
        }
        else
        {
            mright();
            Thread.Sleep(frec);
        }              
    }

}
void map()
{

    if (grado == 0)
    {
        ismoving = false;
        frec = 0;
    }
    else
    {
        int ut = 1000;//1seg
        left = grado > 0;
        grado = Math.Abs(grado);
        frec = ut / grado / 10;
        ismoving = true;
    }


}
private void Window_Closed_1(object sender, EventArgs e)
{
    simulator.Abort();
}
private void Form1_Load(object sender, EventArgs e)
{
    HookManager.MouseWheel += Window_MouseWheel_1;
    simulator = new Thread(new ThreadStart(move));
}
private void Window_MouseWheel_1(object sender, MouseEventArgs e)
{
    centro += e.Delta;
    grado = centro / 120;
    l1.Text = grado + "";
    map();
}
4

0 回答 0