2017-04-02 3 views
0

안녕하세요 저는 타이머가 10 초에 도달하면 프로그램이 ALT + 2를 보낼 vs2012 프로젝트를 만들고 있습니다. F1 만 sendkey로 작업했지만 ALT TAB 또는 ALT + Number로는 작동하지 않습니다. 지금까지 시도한 일부 코드는 작동하지 않습니다.Visual Studio 2012 ALT + 번호 Sendkeys가 작동하지 않습니다.

My.Computer.Keyboard.SendKeys ("% {TAB}"참 :

If mintCount = 16 Then SendKeys.Send("%2") 
    If mintCount = 16 Then SendKeys.Send("%(2)") 
    If mintCount = 16 Then SendKeys.Send("(%2)") 
    If mintCount = 16 Then SendKeys.Send("%{2}") 
    If mintCount = 16 Then SendKeys.Send("%{TAB}") 

어떤 제안이나 팁은

답변

-1

사용 keyboard.sendkeys 대신 .. 정말 감사 감사하겠습니다)

이것은 활성 탭으로 alt 탭을 보내야하며 부울은 키 입력이 처리 될 때까지 응용 프로그램을 일시 중지할지 여부를 지정합니다.

+0

이 문제에 대해 작동하지 않았다? – Alex

+0

슬프게도 이것은 나를 위해 일하지 않았다 – Duckbenok

+0

FYI'SendKeys'와'Keyboard.SendKeys'는 본질적으로 동일합니다. –

0

InputHelper 클래스를 활용할 수 있습니다. It P/WinAPI의 기본 SendInput() 함수를 호출합니다. SendKeys보다 유연하며 더 넓은 세트의 키를 지원합니다!

InputHelper.vb :

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 

사용법 :

InputHelper.SetKeyState(Keys.Alt, False) 'False = Hold ALT key down. 
InputHelper.PressKey(Keys.D2) 'Press "2". 
InputHelper.SetKeyState(Keys.Alt, True) 'True = Release ALT key. 
+0

답장을 보내 주셔서 감사합니다. 미안 해요 나는 여전히 어디에 Vb 프로그래밍 프로그래밍 또는 InputHelper.vb 코드를 만들려면 새로운 무엇입니까? Form1.vb에 붙여 넣을 때 오류가 발생합니다. 나는 별도의 VB 파일을 만들고 form1.vb에 사용 코드를 호출해야한다고 생각하지만 혼란 스럽다. 제발 도와주세요 – Duckbenok

+0

@Snoopy : 예 그것은 별도의 파일 (그러므로'.vb' 확장자)입니다. Visual Studio를 사용한다면'Solution Explorer'에서 프로젝트를 오른쪽 클릭하고'Add> New Item ... '으로 가면 _ ** OR ** _ 키보드의'CTRL + SHIFT + A'를 누르십시오. 열리는 창에서'Class'를 선택하고'InputHelper.vb'라는 이름을 주면, 자동 생성 된 코드를 제거하고 그것을 제 코드로 바꿉니다. –