2016-07-15 7 views
1

他のアプリケーションにキーを送信することで、それらのキーを押し続けることができますか? は、私はそのC#またはVisual Basicのは、私は彼らを知っていれば、私は気にしないVB.NETやC#でキーを押さえてシミュレーションする方法は?

keystate(keys.A) = downのようなもの(ボタンを押したままにする)

keystate(keys.A) = up(ボタンを離し)

  • をしたい
  • ビジュアルスタジオを使用して
+0

である、あなたはキーの状態をチェックし、それに応じて車を移動することができます。 – Mangist

+0

@マンジストこれは自分のゲームではないと私に伝えています。彼は別のゲームにキー押しを送りようとしている。 – Ares

+0

@あなたは正しいかもしれませんが、目的が何であるかはわかりません。 – Mangist

答えて

2

あなたが行に複数のキーを送信する場合は、あなたがキーを押したままにしたい場合は、あなたがのUSER32ライブラリの呼び出しをインポートする必要がSendKeys.Send

https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx

を使用します。

Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer) 

MapVirtualKeyも必要です。 (。ソフトウェア指向の)これは、ハードウェアの不変の仮想キーセットに(ドライバが指向)あなたの物理的なボード上のキーのレイアウトを転送

<DllImport("User32.dll", SetLastError:=False, CallingConvention:=CallingConvention.StdCall, _ 
     CharSet:=CharSet.Auto)> _ 
Public Shared Function MapVirtualKey(ByVal uCode As UInt32, ByVal uMapType As MapVirtualKeyMapTypes) As UInt32 
End Function 

は、それからちょうどこのような何かを:

ここ
Private Sub HoldKeyDown(ByVal key As Byte, ByVal durationInSeconds As Integer) 
    Dim targetTime As DateTime = DateTime.Now().AddSeconds(durationInSeconds) 
    keybd_event(key, MapVirtualKey(key, 0), 0, 0) ' Down 
    While targetTime.Subtract(DateTime.Now()).TotalSeconds > 0 
     Application.DoEvents() 
    End While 
    keybd_event(key, MapVirtualKey(key, 0), 2, 0) ' Up 
End Sub 
+0

MapVirtualKey? – 123neri123

+0

私はそれを更新しました。これがあなたのために働いた場合は、これを合格とマークすることを忘れないでください。 – Ares

+0

DllImportの部分をどこに置くか教えてください。 – 123neri123

0

シンプルなフォームで、フォームの周りを左/右/上/下のキーが押された状態でカーを移動させます。ゲームの更新ループを毎秒30フレームに設定しましたが、別のThread.Sleep()を使用してこれを変更することができます:

"lblCar"という名前のラベルがあります。すべてのゲーム更新ループの位置。

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      ThreadPool.QueueUserWorkItem(new WaitCallback(GameUpdate)); 
     } 

     private bool leftPressed; 
     private bool rightPressed; 
     private bool upPressed; 
     private bool downPressed; 

     private void GameUpdate(object state) 
     { 
      bool gameRunning = true; 

      do 
      { 
       if (leftPressed) 
       { 
        BeginInvoke(new Action(() => { lblCar.Location = new Point(lblCar.Location.X - 1, lblCar.Location.Y); })); 
       } 
       if (rightPressed) 
       { 
        BeginInvoke(new Action(() => { lblCar.Location = new Point(lblCar.Location.X + 1, lblCar.Location.Y); })); 
       } 
       if (upPressed) 
       { 
        BeginInvoke(new Action(() => { lblCar.Location = new Point(lblCar.Location.X, lblCar.Location.Y - 1); })); 
       } 
       if (downPressed) 
       { 
        BeginInvoke(new Action(() => { lblCar.Location = new Point(lblCar.Location.X, lblCar.Location.Y + 1); })); 
       } 

       Thread.Sleep(33); // 30 frames per second 
      } while (gameRunning); 
     } 

     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      switch (e.KeyCode) 
      { 
       case Keys.Left: leftPressed = true; break; 
       case Keys.Right: rightPressed = true; break; 
       case Keys.Up: upPressed = true; break; 
       case Keys.Down: downPressed = true; break; 
       default: break; 
      } 
     } 

     private void Form1_KeyUp(object sender, KeyEventArgs e) 
     { 
      switch (e.KeyCode) 
      { 
       case Keys.Left: leftPressed = false; break; 
       case Keys.Right: rightPressed = false; break; 
       case Keys.Up: upPressed = false; break; 
       case Keys.Down: downPressed = false; break; 
       default: break; 
      } 
     } 
    } 
0

(visulスタジオ)コードは、あなたのゲームのアップデート()ループでは

Imports System.Runtime.InteropServices 

Public Class Form1 
    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer) 


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    End Sub 

    <DllImport("User32.dll", SetLastError:=False, CallingConvention:=CallingConvention.StdCall, _ 
      CharSet:=CharSet.Auto)> _ 
    Public Shared Function MapVirtualKey(ByVal uCode As UInt32, ByVal uMapType As UInt32) As UInt32 
    End Function 

    Private Sub HoldKeyDown(ByVal key As Byte, ByVal durationInSeconds As Integer) 
     Dim targetTime As DateTime = DateTime.Now().AddSeconds(durationInSeconds) 
     keybd_event(key, MapVirtualKey(key, 0), 0, 0) ' Down 
     While targetTime.Subtract(DateTime.Now()).TotalSeconds > 0 
      Application.DoEvents() 
     End While 
     keybd_event(key, MapVirtualKey(key, 0), 2, 0) ' Up 
    End Sub 

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
     HoldKeyDown(Keys.A, 5) 
    End Sub 
End Class 
関連する問題