簡體   English   中英

在C#WPF中模擬箭頭鍵按下

[英]Simulate arrows key press in C# WPF

當IF條件為真時,我想模擬向左和向右箭頭按鍵。 到目前為止,到目前為止,我已經嘗試過了,但是沒有用。 我還使用win32.dll導入方法控制鼠標。 如果我使用SendKeys.Send(“ LEFT”); 方法,我的鼠標移動不受控制。 我覺得此調用后代碼未運行。

我在頂部包含了system.windows.forms,我在做什么錯?

如何以簡單的方式模擬箭頭鍵?

     if (shoulderLeft.Position.Z > shoulderRight.Position.Z)
                            {
                                status.Fill = new SolidColorBrush(System.Windows.Media.Colors.DarkBlue);
                               SendKeys.Send("LEFT");
                               // System.Windows.Forms.MessageBox.Show("Left ROTATION");


                            }
                                //right rotate
                            else if ((shoulderLeft.Position.Z < shoulderRight.Position.Z))
                            {
                                SendKeys.Send("RIGHT");
                                status.Fill = new SolidColorBrush(System.Windows.Media.Colors.Red);

                            }

您應該在代碼中在LEFTRIGHT加上方括號:

SendKeys.Send("{LEFT}");

這是完整的代碼列表

SendKeys.Send("{LEFT}");
SendKeys.Send("{RIGHT}");

模擬輸入事件通常不是一個好習慣,而是將在事件處理程序中運行的代碼包裝在參數化方法中,然后在要模擬事件時調用該函數。 例如(稍作):

void myForm_MouseMoved(object sender, MouseEventArgs e)
{
    MoveCharacter(e.X, e.Y);
}

void myForm_SomethingElseHappened(object sender, EventArgs e)
{
    if(IsCharacterTooLow()) 
    {            
        MoveCharacter(_currentPos.X, _currentPos.Y+20)
    }
}

希望這是有用的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM