2016-11-29 5 views
0

これは問題ではありません。私は私のジョイスティック(DXボタンを送る)のキーを押すことを可能にするルーチンをコード化しようとしており、同時にキーを押し下げて保持することをシミュレートしている。基本的には3行のコードになっています。キーを押してから解放する

10. Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) 

    20. If (condition=true) then 
    30.  keybd_event(Keys.Scroll, 0, 0, 0) 
    40. Else 
    50.  keybd_event(Keys.Scroll, 0, 2, 0) 
    60. EndIf 

明瞭にするために行番号が追加されました。ご覧のように、20行目はSCROLL LOCKキーを保持し、30行目はSCROLL LOCKキーを再び押し続けます。このコードは私のニーズに完全に対応していますが(1時間35分のセッションで、Falcon BMS 4.33U2、IVC Client、およびFRAPSでは何の問題も経験しませんでした)、これを動作させるには、Debug> Exceptions>マネージドデバッグアシスタント> PInvokeStackImbalance(スロー)。

私の質問です - これはプログラムにとって「安全な」方法ですか、言い換えれば、私はこれを動作させるためにどこかで気にしましたか?それが「安全」でない場合、同じことをする適切な方法がありますか?

+0

''宣言


は本日方法SendInput()推奨使用するには、私のInputHelperクラスを利用することができます。 ..Lib'。キーボード/マウスイベントを送信する現在の方法は、[** 'SendInput()' **](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v = vs。 85).aspx)。私のキーボード入力ラッパーを参照してください[**この回答**](http://stackoverflow.com/questions/39809095/why-do-some-applications-not-accept-some-sendkeys-at-some-times/39811061 #39811061)。 –

+0

この例を見てください: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx これはnumloc用ですが、あなたはKEYEVENTF_EXTENDEDKEYを使うべきです。 2番目:COnditionの後ろに何がありますか?それは次のようなものです: GetKeyboardState((LPBYTE)&keyState);例えば。現在のキー状態を取得するには? keybg_eventが代わりに – Kiko

+0

になっているので、keybg_eventの代わりにSetInputを使用することができます。関数が正しく宣言されていれば、エラーはまったくないはずです。例外が処理されてもアプリケーションが中断されるため、[Thrown]チェックボックスにはデフォルトで何も選択されていないはずです。 –

答えて

0

私のコメントで述べたように、keybd_event()は非推奨であり、SendInput()で置き換えられました。しかし、PInvokeStackImbalance例外を受け取っている理由は、関数宣言の最後の2つのパラメーターが間違っているためです。

ほとんどすべてDeclare...Lib VB6以下の例があります。たとえば、LongはVB.NETのLongと同じではありません。 いつもに連絡してください。DllImport attributeを使ってP/Invokeスニペットを探してください。 VB.NETバージョンが見つからない場合は、C#スニペットを探して、オンラインコンバータを使用して変換できます。

最後の2つのパラメータは、UIntegerUIntPtrである必要があります。私が言ったように

Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UIntPtr) 

しかし、DllImportに固執することをお勧めします:あなたは/ Pのためpinvoke.netをチェックスニペットを呼び出すことができます

<DllImport("user32.dll")> _ 
Public Shared Sub keybd_event(bVk As Byte, bScan As Byte, dwFlags As UInteger, dwExtraInfo As UIntPtr) 
End Function 

。例えば

Imports System.Runtime.InteropServices 

Public NotInheritable Class InputHelper 
    Private Sub New() 
    End Sub 

#Region "Methods" 
#Region "PressKey()" 
    ''' <summary> 
    ''' Virtually presses a key. 
    ''' </summary> 
    ''' <param name="Key">The key to press.</param> 
    ''' <param name="HardwareKey">Whether or not to press the key using its hardware scan code.</param> 
    ''' <remarks></remarks> 
    Public Shared Sub PressKey(ByVal Key As Keys, Optional ByVal HardwareKey As Boolean = False) 
     If HardwareKey = False Then 
      InputHelper.SetKeyState(Key, False) 
      InputHelper.SetKeyState(Key, True) 
     Else 
      InputHelper.SetHardwareKeyState(Key, False) 
      InputHelper.SetHardwareKeyState(Key, True) 
     End If 
    End Sub 
#End Region 

#Region "SetKeyState()" 
    ''' <summary> 
    ''' Virtually sends a key event. 
    ''' </summary> 
    ''' <param name="Key">The key of the event to send.</param> 
    ''' <param name="KeyUp">Whether to push down or release the key.</param> 
    ''' <remarks></remarks> 
    Public Shared Sub SetKeyState(ByVal Key As Keys, ByVal KeyUp As Boolean) 
     Key = ReplaceBadKeys(Key) 

     Dim KeyboardInput As New KEYBDINPUT With { 
      .wVk = Key, 
      .wScan = 0, 
      .time = 0, 
      .dwFlags = If(KeyUp, KEYEVENTF.KEYUP, 0), 
      .dwExtraInfo = IntPtr.Zero 
     } 

     Dim Union As New INPUTUNION With {.ki = KeyboardInput} 
     Dim Input As New INPUT With { 
      .type = INPUTTYPE.KEYBOARD, 
      .U = Union 
     } 

     SendInput(1, New INPUT() {Input}, Marshal.SizeOf(GetType(INPUT))) 
    End Sub 
#End Region 

#Region "SetHardwareKeyState()" 
    ''' <summary> 
    ''' Virtually sends a key event using the key's scan code. 
    ''' </summary> 
    ''' <param name="Key">The key of the event to send.</param> 
    ''' <param name="KeyUp">Whether to push down or release the key.</param> 
    ''' <remarks></remarks> 
    Public Shared Sub SetHardwareKeyState(ByVal Key As Keys, ByVal KeyUp As Boolean) 
     Key = ReplaceBadKeys(Key) 

     Dim KeyboardInput As New KEYBDINPUT With { 
      .wVk = 0, 
      .wScan = MapVirtualKeyEx(CUInt(Key), 0, GetKeyboardLayout(0)), 
      .time = 0, 
      .dwFlags = KEYEVENTF.SCANCODE Or If(KeyUp, KEYEVENTF.KEYUP, 0), 
      .dwExtraInfo = IntPtr.Zero 
     } 

     Dim Union As New INPUTUNION With {.ki = KeyboardInput} 
     Dim Input As New INPUT With { 
      .type = INPUTTYPE.KEYBOARD, 
      .U = Union 
     } 

     SendInput(1, New INPUT() {Input}, Marshal.SizeOf(GetType(INPUT))) 
    End Sub 
#End Region 

#Region "ReplaceBadKeys()" 
    ''' <summary> 
    ''' Replaces bad keys with their corresponding VK_* value. 
    ''' </summary> 
    ''' <remarks></remarks> 
    Private Shared Function ReplaceBadKeys(ByVal Key As Keys) As Keys 
     Dim ReturnValue As Keys = Key 

     If ReturnValue.HasFlag(Keys.Control) Then 
      ReturnValue = (ReturnValue And Not Keys.Control) Or Keys.ControlKey 'Replace Keys.Control with Keys.ControlKey. 
     End If 

     If ReturnValue.HasFlag(Keys.Shift) Then 
      ReturnValue = (ReturnValue And Not Keys.Shift) Or Keys.ShiftKey 'Replace Keys.Shift with Keys.ShiftKey. 
     End If 

     If ReturnValue.HasFlag(Keys.Alt) Then 
      ReturnValue = (ReturnValue And Not Keys.Alt) Or Keys.Menu 'Replace Keys.Alt with Keys.Menu. 
     End If 

     Return ReturnValue 
    End Function 
#End Region 
#End Region 

#Region "WinAPI P/Invokes" 
    <DllImport("user32.dll", SetLastError:=True)> 
    Private Shared Function SendInput(ByVal nInputs As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal pInputs() As INPUT, ByVal cbSize As Integer) As UInteger 
    End Function 

    <DllImport("user32.dll")> _ 
    Private Shared Function MapVirtualKeyEx(uCode As UInteger, uMapType As UInteger, dwhkl As IntPtr) As UInteger 
    End Function 

    <DllImport("user32.dll")> _ 
    Private Shared Function GetKeyboardLayout(idThread As UInteger) As IntPtr 
    End Function 

#Region "Enumerations" 
    Private Enum INPUTTYPE As UInteger 
     MOUSE = 0 
     KEYBOARD = 1 
     HARDWARE = 2 
    End Enum 

    <Flags()> _ 
    Private Enum KEYEVENTF As UInteger 
     EXTENDEDKEY = &H1 
     KEYUP = &H2 
     SCANCODE = &H8 
     UNICODE = &H4 
    End Enum 
#End Region 

#Region "Structures" 
    <StructLayout(LayoutKind.Explicit)> _ 
    Private Structure INPUTUNION 
     <FieldOffset(0)> Public mi As MOUSEINPUT 
     <FieldOffset(0)> Public ki As KEYBDINPUT 
     <FieldOffset(0)> Public hi As HARDWAREINPUT 
    End Structure 

    Private Structure INPUT 
     Public type As Integer 
     Public U As INPUTUNION 
    End Structure 

    Private Structure MOUSEINPUT 
     Public dx As Integer 
     Public dy As Integer 
     Public mouseData As Integer 
     Public dwFlags As Integer 
     Public time As Integer 
     Public dwExtraInfo As IntPtr 
    End Structure 

    Private Structure KEYBDINPUT 
     Public wVk As UShort 
     Public wScan As Short 
     Public dwFlags As UInteger 
     Public time As Integer 
     Public dwExtraInfo As IntPtr 
    End Structure 

    Private Structure HARDWAREINPUT 
     Public uMsg As Integer 
     Public wParamL As Short 
     Public wParamH As Short 
    End Structure 
#End Region 
#End Region 
End Class 

は廃止、などですkeybd_event`
If condition = True Then 
    InputHelper.SetKeyState(Keys.Scroll, False) 'Key down. 
Else 
    InputHelper.SetKeyState(Keys.Scroll, True) 'Key up. 
End If 
関連する問題