elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  sendkeys() con teclas especiales
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: sendkeys() con teclas especiales  (Leído 3,356 veces)
El Benjo


Desconectado Desconectado

Mensajes: 392



Ver Perfil WWW
sendkeys() con teclas especiales
« en: 23 Abril 2014, 21:45 pm »

He podido utilizar la función sendkeys() sin ningún problema, incluyendo teclas como el tabulador, shift, alt, etc. Pero me gustaría también poder enviar otro tipo de teclas como por ejemplo las teclas multimedia de algunos teclados.

¿Es posible realizar esto directamente en .NET o necesito hacer uso de la API de Windows?


« Última modificación: 25 Abril 2014, 18:36 pm por El Benjo » En línea

www.es.neftis-ai.com

Sí hay un mejor lenguaje de programación y es ese con el que puedes desarrollar tus objetivos.
Eleкtro
Ex-Staff
*
Conectado Conectado

Mensajes: 9.878



Ver Perfil
Re: sendkeys() con teclas especiales
« Respuesta #1 en: 25 Abril 2014, 08:20 am »

Desconozco cuales son las "abreviaciones" de las teclas multimedia para el método SendKey (o tampoco si existen), pero puedes usar la API SendInput.

Dale una oportunidad a mi método 'SendKey' (lo malo es que no tengo un teclado multimedia para testear lo que comentas):

Código
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 02-21-2014
  4. ' ***********************************************************************
  5. ' <copyright file="SendInputs.vb" company="Elektro Studios">
  6. '     Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. #Region " Usage Examples "
  11.  
  12. 'Private Sub Test() Handles Button1.Click
  13.  
  14. ' AppActivate(Process.GetProcessesByName("notepad").First.Id)
  15.  
  16. ' Dim c As Char = Convert.ToChar(Keys.Oemtilde) ' Ñ
  17. ' Dim Result As Integer = SendInputs.SendKey(Convert.ToChar(c.ToString.ToLower))
  18. ' MessageBox.Show(String.Format("Successfull events: {0}", CStr(Result)))
  19.  
  20. ' SendInputs.SendKey(Keys.Enter)
  21. ' SendInputs.SendKey(Convert.ToChar(Keys.Back))
  22. ' SendInputs.SendKeys("Hello World", True)
  23. ' SendInputs.SendKey(Convert.ToChar(Keys.D0))
  24. ' SendInputs.SendKeys(Keys.Insert, BlockInput:=True)
  25.  
  26. ' SendInputs.MouseClick(SendInputs.MouseButton.RightPress, False)
  27. ' SendInputs.MouseMove(5, -5)
  28. ' SendInputs.MousePosition(New Point(100, 500))
  29.  
  30. 'End Sub
  31.  
  32. #End Region
  33.  
  34. #Region " Imports "
  35.  
  36. Imports System.Runtime.InteropServices
  37. Imports System.ComponentModel
  38.  
  39. #End Region
  40.  
  41. ''' <summary>
  42. ''' Synthesizes keystrokes, mouse motions, and button clicks.
  43. ''' </summary>
  44. Public Class SendInputs
  45.  
  46. #Region " P/Invoke "
  47.  
  48.   Friend Class NativeMethods
  49.  
  50. #Region " Methods "
  51.  
  52.       ''' <summary>
  53.       ''' Blocks keyboard and mouse input events from reaching applications.
  54.       ''' For more info see here:
  55.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646290%28v=vs.85%29.aspx
  56.       ''' </summary>
  57.       ''' <param name="fBlockIt">
  58.       ''' The function's purpose.
  59.       ''' If this parameter is 'TRUE', keyboard and mouse input events are blocked.
  60.       ''' If this parameter is 'FALSE', keyboard and mouse events are unblocked.
  61.       ''' </param>
  62.       ''' <returns>
  63.       ''' If the function succeeds, the return value is nonzero.
  64.       ''' If input is already blocked, the return value is zero.
  65.       ''' </returns>
  66.       ''' <remarks>
  67.       ''' Note that only the thread that blocked input can successfully unblock input.
  68.       ''' </remarks>
  69.       <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall,
  70.       SetLastError:=True)>
  71.       Friend Shared Function BlockInput(
  72.              ByVal fBlockIt As Boolean
  73.       ) As Integer
  74.       End Function
  75.  
  76.       ''' <summary>
  77.       ''' Synthesizes keystrokes, mouse motions, and button clicks.
  78.       ''' For more info see here:
  79.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx
  80.       ''' </summary>
  81.       ''' <param name="nInputs">
  82.       ''' Indicates the number of structures in the pInputs array.
  83.       ''' </param>
  84.       ''' <param name="pInputs">
  85.       ''' Indicates an Array of 'INPUT' structures.
  86.       ''' Each structure represents an event to be inserted into the keyboard or mouse input stream.
  87.       ''' </param>
  88.       ''' <param name="cbSize">
  89.       ''' The size, in bytes, of an 'INPUT' structure.
  90.       ''' If 'cbSize' is not the size of an 'INPUT' structure, the function fails.
  91.       ''' </param>
  92.       ''' <returns>
  93.       ''' The function returns the number of events that it successfully
  94.       ''' inserted into the keyboard or mouse input stream.
  95.       ''' If the function returns zero, the input was already blocked by another thread.
  96.       ''' </returns>
  97.       <DllImport("user32.dll", SetLastError:=True)>
  98.       Friend Shared Function SendInput(
  99.              ByVal nInputs As Integer,
  100.              <MarshalAs(UnmanagedType.LPArray), [In]> ByVal pInputs As INPUT(),
  101.              ByVal cbSize As Integer
  102.       ) As Integer
  103.       End Function
  104.  
  105. #End Region
  106.  
  107. #Region " Enumerations "
  108.  
  109.       ''' <summary>
  110.       ''' VirtualKey codes.
  111.       ''' </summary>
  112.       Friend Enum VirtualKeys As Short
  113.  
  114.           ''' <summary>
  115.           ''' The Shift key.
  116.           ''' VK_SHIFT
  117.           ''' </summary>
  118.           SHIFT = &H10S
  119.  
  120.           ''' <summary>
  121.           ''' The DEL key.
  122.           ''' VK_DELETE
  123.           ''' </summary>
  124.           DELETE = 46S
  125.  
  126.           ''' <summary>
  127.           ''' The ENTER key.
  128.           ''' VK_RETURN
  129.           ''' </summary>
  130.           [RETURN] = 13S
  131.  
  132.       End Enum
  133.  
  134.       ''' <summary>
  135.       ''' The type of the input event.
  136.       ''' For more info see here:
  137.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
  138.       ''' </summary>
  139.       <Description("Enumeration used for 'type' parameter of 'INPUT' structure")>
  140.       Friend Enum InputType As Integer
  141.  
  142.           ''' <summary>
  143.           ''' The event is a mouse event.
  144.           ''' Use the mi structure of the union.
  145.           ''' </summary>
  146.           Mouse = 0
  147.  
  148.           ''' <summary>
  149.           ''' The event is a keyboard event.
  150.           ''' Use the ki structure of the union.
  151.           ''' </summary>
  152.           Keyboard = 1
  153.  
  154.           ''' <summary>
  155.           ''' The event is a hardware event.
  156.           ''' Use the hi structure of the union.
  157.           ''' </summary>
  158.           Hardware = 2
  159.  
  160.       End Enum
  161.  
  162.       ''' <summary>
  163.       ''' Specifies various aspects of a keystroke.
  164.       ''' This member can be certain combinations of the following values.
  165.       ''' For more info see here:
  166.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
  167.       ''' </summary>
  168.       <Description("Enumeration used for 'dwFlags' parameter of 'KeyboardInput' structure")>
  169.       <Flags>
  170.       Friend Enum KeyboardInput_Flags As Integer
  171.  
  172.           ''' <summary>
  173.           ''' If specified, the scan code was preceded by a prefix byte that has the value '0xE0' (224).
  174.           ''' </summary>
  175.           ExtendedKey = &H1
  176.  
  177.           ''' <summary>
  178.           ''' If specified, the key is being pressed.
  179.           ''' </summary>
  180.           KeyDown = &H0
  181.  
  182.           ''' <summary>
  183.           ''' If specified, the key is being released.
  184.           ''' If not specified, the key is being pressed.
  185.           ''' </summary>
  186.           KeyUp = &H2
  187.  
  188.           ''' <summary>
  189.           ''' If specified, 'wScan' identifies the key and 'wVk' is ignored.
  190.           ''' </summary>
  191.           ScanCode = &H8
  192.  
  193.           ''' <summary>
  194.           ''' If specified, the system synthesizes a 'VK_PACKET' keystroke.
  195.           ''' The 'wVk' parameter must be '0'.
  196.           ''' This flag can only be combined with the 'KEYEVENTF_KEYUP' flag.
  197.           ''' </summary>
  198.           Unicode = &H4
  199.  
  200.       End Enum
  201.  
  202.       ''' <summary>
  203.       ''' A set of bit flags that specify various aspects of mouse motion and button clicks.
  204.       ''' The bits in this member can be any reasonable combination of the following values.
  205.       ''' For more info see here:
  206.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273%28v=vs.85%29.aspx
  207.       ''' </summary>
  208.       <Description("Enumeration used for 'dwFlags' parameter of 'MouseInput' structure")>
  209.       <Flags>
  210.       Friend Enum MouseInput_Flags As Integer
  211.  
  212.           ''' <summary>
  213.           ''' The 'dx' and 'dy' members contain normalized absolute coordinates.
  214.           ''' If the flag is not set, 'dx' and 'dy' contain relative data
  215.           ''' (the change in position since the last reported position).
  216.           ''' This flag can be set, or not set,
  217.           ''' regardless of what kind of mouse or other pointing device, if any, is connected to the system.
  218.           ''' </summary>
  219.           Absolute = &H8000I
  220.  
  221.           ''' <summary>
  222.           ''' Movement occurred.
  223.           ''' </summary>
  224.           Move = &H1I
  225.  
  226.           ''' <summary>
  227.           ''' The 'WM_MOUSEMOVE' messages will not be coalesced.
  228.           ''' The default behavior is to coalesce 'WM_MOUSEMOVE' messages.
  229.           ''' </summary>
  230.           Move_NoCoalesce = &H2000I
  231.  
  232.           ''' <summary>
  233.           ''' The left button was pressed.
  234.           ''' </summary>
  235.           LeftDown = &H2I
  236.  
  237.           ''' <summary>
  238.           ''' The left button was released.
  239.           ''' </summary>
  240.           LeftUp = &H4I
  241.  
  242.           ''' <summary>
  243.           ''' The right button was pressed.
  244.           ''' </summary>
  245.           RightDown = &H8I
  246.  
  247.           ''' <summary>
  248.           ''' The right button was released.
  249.           ''' </summary>
  250.           RightUp = &H10I
  251.  
  252.           ''' <summary>
  253.           ''' The middle button was pressed.
  254.           ''' </summary>
  255.           MiddleDown = &H20I
  256.  
  257.           ''' <summary>
  258.           ''' The middle button was released.
  259.           ''' </summary>
  260.           MiddleUp = &H40I
  261.  
  262.           ''' <summary>
  263.           ''' Maps coordinates to the entire desktop.
  264.           ''' Must be used in combination with 'Absolute'.
  265.           ''' </summary>
  266.           VirtualDesk = &H4000I
  267.  
  268.           ''' <summary>
  269.           ''' The wheel was moved, if the mouse has a wheel.
  270.           ''' The amount of movement is specified in 'mouseData'.
  271.           ''' </summary>
  272.           Wheel = &H800I
  273.  
  274.           ''' <summary>
  275.           ''' The wheel was moved horizontally, if the mouse has a wheel.
  276.           ''' The amount of movement is specified in 'mouseData'.
  277.           ''' </summary>
  278.           HWheel = &H1000I
  279.  
  280.           ''' <summary>
  281.           ''' An X button was pressed.
  282.           ''' </summary>
  283.           XDown = &H80I
  284.  
  285.           ''' <summary>
  286.           ''' An X button was released.
  287.           ''' </summary>
  288.           XUp = &H100I
  289.  
  290.       End Enum
  291.  
  292. #End Region
  293.  
  294. #Region " Structures "
  295.  
  296.       ''' <summary>
  297.       ''' Used by 'SendInput' function
  298.       ''' to store information for synthesizing input events such as keystrokes, mouse movement, and mouse clicks.
  299.       ''' For more info see here:
  300.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
  301.       ''' </summary>
  302.       <Description("Structure used for 'INPUT' parameter of 'SendInput' API method")>
  303.       <StructLayout(LayoutKind.Explicit)>
  304.       Friend Structure Input
  305.  
  306.           ' ******
  307.           '  NOTE
  308.           ' ******
  309.           ' Field offset for 32 bit machine: 4
  310.           ' Field offset for 64 bit machine: 8
  311.  
  312.           ''' <summary>
  313.           ''' The type of the input event.
  314.           ''' </summary>
  315.           <FieldOffset(0)>
  316.           Public type As InputType
  317.  
  318.           ''' <summary>
  319.           ''' The information about a simulated mouse event.
  320.           ''' </summary>
  321.           <FieldOffset(8)>
  322.           Public mi As MouseInput
  323.  
  324.           ''' <summary>
  325.           ''' The information about a simulated keyboard event.
  326.           ''' </summary>
  327.           <FieldOffset(8)>
  328.           Public ki As KeyboardInput
  329.  
  330.           ''' <summary>
  331.           ''' The information about a simulated hardware event.
  332.           ''' </summary>
  333.           <FieldOffset(8)>
  334.           Public hi As HardwareInput
  335.  
  336.       End Structure
  337.  
  338.       ''' <summary>
  339.       ''' Contains information about a simulated mouse event.
  340.       ''' For more info see here:
  341.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273%28v=vs.85%29.aspx
  342.       ''' </summary>
  343.       <Description("Structure used for 'mi' parameter of 'INPUT' structure")>
  344.       Friend Structure MouseInput
  345.  
  346.           ''' <summary>
  347.           ''' The absolute position of the mouse,
  348.           ''' or the amount of motion since the last mouse event was generated,
  349.           ''' depending on the value of the dwFlags member.
  350.           ''' Absolute data is specified as the 'x' coordinate of the mouse;
  351.           ''' relative data is specified as the number of pixels moved.
  352.           ''' </summary>
  353.           Public dx As Integer
  354.  
  355.           ''' <summary>
  356.           ''' The absolute position of the mouse,
  357.           ''' or the amount of motion since the last mouse event was generated,
  358.           ''' depending on the value of the dwFlags member.
  359.           ''' Absolute data is specified as the 'y' coordinate of the mouse;
  360.           ''' relative data is specified as the number of pixels moved.
  361.           ''' </summary>
  362.           Public dy As Integer
  363.  
  364.           ''' <summary>
  365.           ''' If 'dwFlags' contains 'MOUSEEVENTF_WHEEL',
  366.           ''' then 'mouseData' specifies the amount of wheel movement.
  367.           ''' A positive value indicates that the wheel was rotated forward, away from the user;
  368.           ''' a negative value indicates that the wheel was rotated backward, toward the user.
  369.           ''' One wheel click is defined as 'WHEEL_DELTA', which is '120'.
  370.           '''
  371.           ''' If 'dwFlags' does not contain 'MOUSEEVENTF_WHEEL', 'MOUSEEVENTF_XDOWN', or 'MOUSEEVENTF_XUP',
  372.           ''' then mouseData should be '0'.
  373.           ''' </summary>
  374.           Public mouseData As Integer
  375.  
  376.           ''' <summary>
  377.           ''' A set of bit flags that specify various aspects of mouse motion and button clicks.
  378.           ''' The bits in this member can be any reasonable combination of the following values.
  379.           ''' The bit flags that specify mouse button status are set to indicate changes in status,
  380.           ''' not ongoing conditions.
  381.           ''' For example, if the left mouse button is pressed and held down,
  382.           ''' 'MOUSEEVENTF_LEFTDOWN' is set when the left button is first pressed,
  383.           ''' but not for subsequent motions.
  384.           ''' Similarly, 'MOUSEEVENTF_LEFTUP' is set only when the button is first released.
  385.           '''
  386.           ''' You cannot specify both the 'MOUSEEVENTF_WHEE'L flag
  387.           ''' and either 'MOUSEEVENTF_XDOWN' or 'MOUSEEVENTF_XUP' flags simultaneously in the 'dwFlags' parameter,
  388.           ''' because they both require use of the 'mouseData' field.
  389.           ''' </summary>
  390.           Public dwFlags As MouseInput_Flags
  391.  
  392.           ''' <summary>
  393.           ''' The time stamp for the event, in milliseconds.
  394.           ''' If this parameter is '0', the system will provide its own time stamp.
  395.           ''' </summary>
  396.           Public time As Integer
  397.  
  398.           ''' <summary>
  399.           ''' An additional value associated with the mouse event.
  400.           ''' An application calls 'GetMessageExtraInfo' to obtain this extra information.
  401.           ''' </summary>
  402.           Public dwExtraInfo As IntPtr
  403.  
  404.       End Structure
  405.  
  406.       ''' <summary>
  407.       ''' Contains information about a simulated keyboard event.
  408.       ''' For more info see here:
  409.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
  410.       ''' </summary>
  411.       <Description("Structure used for 'ki' parameter of 'INPUT' structure")>
  412.       Friend Structure KeyboardInput
  413.  
  414.           ''' <summary>
  415.           ''' A virtual-key code.
  416.           ''' The code must be a value in the range '1' to '254'.
  417.           ''' If the 'dwFlags' member specifies 'KEYEVENTF_UNICODE', wVk must be '0'.
  418.           ''' </summary>
  419.           Public wVk As Short
  420.  
  421.           ''' <summary>
  422.           ''' A hardware scan code for the key.
  423.           ''' If 'dwFlags' specifies 'KEYEVENTF_UNICODE',
  424.           ''' 'wScan' specifies a Unicode character which is to be sent to the foreground application.
  425.           ''' </summary>
  426.           Public wScan As Short
  427.  
  428.           ''' <summary>
  429.           ''' Specifies various aspects of a keystroke.
  430.           ''' </summary>
  431.           Public dwFlags As KeyboardInput_Flags
  432.  
  433.           ''' <summary>
  434.           ''' The time stamp for the event, in milliseconds.
  435.           ''' If this parameter is '0', the system will provide its own time stamp.
  436.           ''' </summary>
  437.           Public time As Integer
  438.  
  439.           ''' <summary>
  440.           ''' An additional value associated with the keystroke.
  441.           ''' Use the 'GetMessageExtraInfo' function to obtain this information.
  442.           ''' </summary>
  443.           Public dwExtraInfo As IntPtr
  444.  
  445.       End Structure
  446.  
  447.       ''' <summary>
  448.       ''' Contains information about a simulated message generated by an input device other than a keyboard or mouse.
  449.       ''' For more info see here:
  450.       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646269%28v=vs.85%29.aspx
  451.       ''' </summary>
  452.       <Description("Structure used for 'hi' parameter of 'INPUT' structure")>
  453.       Friend Structure HardwareInput
  454.  
  455.           ''' <summary>
  456.           ''' The message generated by the input hardware.
  457.           ''' </summary>
  458.           Public uMsg As Integer
  459.  
  460.           ''' <summary>
  461.           ''' The low-order word of the lParam parameter for uMsg.
  462.           ''' </summary>
  463.           Public wParamL As Short
  464.  
  465.           ''' <summary>
  466.           ''' The high-order word of the lParam parameter for uMsg.
  467.           ''' </summary>
  468.           Public wParamH As Short
  469.  
  470.       End Structure
  471.  
  472. #End Region
  473.  
  474.   End Class
  475.  
  476. #End Region
  477.  
  478. #Region " Enumerations "
  479.  
  480.   ''' <summary>
  481.   ''' Indicates a mouse button.
  482.   ''' </summary>
  483.   <Description("Enumeration used for 'MouseAction' parameter of 'MouseClick' function.")>
  484.   Public Enum MouseButton As Integer
  485.  
  486.       ''' <summary>
  487.       ''' Hold the left button.
  488.       ''' </summary>
  489.       LeftDown = &H2I
  490.  
  491.       ''' <summary>
  492.       ''' Release the left button.
  493.       ''' </summary>
  494.       LeftUp = &H4I
  495.  
  496.       ''' <summary>
  497.       ''' Hold the right button.
  498.       ''' </summary>
  499.       RightDown = &H8I
  500.  
  501.       ''' <summary>
  502.       ''' Release the right button.
  503.       ''' </summary>
  504.       RightUp = &H10I
  505.  
  506.       ''' <summary>
  507.       ''' Hold the middle button.
  508.       ''' </summary>
  509.       MiddleDown = &H20I
  510.  
  511.       ''' <summary>
  512.       ''' Release the middle button.
  513.       ''' </summary>
  514.       MiddleUp = &H40I
  515.  
  516.       ''' <summary>
  517.       ''' Press the left button.
  518.       ''' ( Hold + Release )
  519.       ''' </summary>
  520.       LeftPress = LeftDown + LeftUp
  521.  
  522.       ''' <summary>
  523.       ''' Press the Right button.
  524.       ''' ( Hold + Release )
  525.       ''' </summary>
  526.       RightPress = RightDown + RightUp
  527.  
  528.       ''' <summary>
  529.       ''' Press the Middle button.
  530.       ''' ( Hold + Release )
  531.       ''' </summary>
  532.       MiddlePress = MiddleDown + MiddleUp
  533.  
  534.   End Enum
  535.  
  536. #End Region
  537.  
  538. #Region " Public Methods "
  539.  
  540.   ''' <summary>
  541.   ''' Sends a keystroke.
  542.   ''' </summary>
  543.   ''' <param name="key">
  544.   ''' Indicates the keystroke to simulate.
  545.   ''' </param>
  546.   ''' <param name="BlockInput">
  547.   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
  548.   ''' </param>
  549.   ''' <returns>
  550.   ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
  551.   ''' If the function returns zero, the input was already blocked by another thread.
  552.   ''' </returns>
  553.   Public Shared Function SendKey(ByVal key As Char,
  554.                                  Optional BlockInput As Boolean = False) As Integer
  555.  
  556.       ' Block Keyboard and mouse.
  557.       If BlockInput Then NativeMethods.BlockInput(True)
  558.  
  559.       ' The inputs structures to send.
  560.       Dim Inputs As New List(Of NativeMethods.INPUT)
  561.  
  562.       ' The current input to add into the Inputs list.
  563.       Dim CurrentInput As New NativeMethods.INPUT
  564.  
  565.       ' Determines whether a character is an alphabetic letter.
  566.       Dim IsAlphabetic As Boolean = Not (key.ToString.ToUpper = key.ToString.ToLower)
  567.  
  568.       ' Determines whether a character is an uppercase alphabetic letter.
  569.       Dim IsUpperCase As Boolean =
  570.           (key.ToString = key.ToString.ToUpper) AndAlso Not (key.ToString.ToUpper = key.ToString.ToLower)
  571.  
  572.       ' Determines whether the CapsLock key is pressed down.
  573.       Dim CapsLockON As Boolean = My.Computer.Keyboard.CapsLock
  574.  
  575.       ' Set the passed key to upper-case.
  576.       If IsAlphabetic AndAlso Not IsUpperCase Then
  577.           key = Convert.ToChar(key.ToString.ToUpper)
  578.       End If
  579.  
  580.       ' If character is alphabetic and is UpperCase and CapsLock is pressed down,
  581.       ' OrElse character is alphabetic and is not UpperCase and CapsLock is not pressed down,
  582.       ' OrElse character is not alphabetic.
  583.       If (IsAlphabetic AndAlso IsUpperCase AndAlso CapsLockON) _
  584.       OrElse (IsAlphabetic AndAlso Not IsUpperCase AndAlso Not CapsLockON) _
  585.       OrElse (Not IsAlphabetic) Then
  586.  
  587.           ' Hold the character key.
  588.           With CurrentInput
  589.               .type = NativeMethods.InputType.Keyboard
  590.               .ki.wVk = Convert.ToInt16(CChar(key))
  591.               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
  592.           End With : Inputs.Add(CurrentInput)
  593.  
  594.           ' Release the character key.
  595.           With CurrentInput
  596.               .type = NativeMethods.InputType.Keyboard
  597.               .ki.wVk = Convert.ToInt16(CChar(key))
  598.               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
  599.           End With : Inputs.Add(CurrentInput)
  600.  
  601.           ' If character is alphabetic and is UpperCase and CapsLock is not pressed down,
  602.           ' OrElse character is alphabetic and is not UpperCase and CapsLock is pressed down.
  603.       ElseIf (IsAlphabetic AndAlso IsUpperCase AndAlso Not CapsLockON) _
  604.       OrElse (IsAlphabetic AndAlso Not IsUpperCase AndAlso CapsLockON) Then
  605.  
  606.           ' Hold the Shift key.
  607.           With CurrentInput
  608.               .type = NativeMethods.InputType.Keyboard
  609.               .ki.wVk = NativeMethods.VirtualKeys.SHIFT
  610.               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
  611.           End With : Inputs.Add(CurrentInput)
  612.  
  613.           ' Hold the character key.
  614.           With CurrentInput
  615.               .type = NativeMethods.InputType.Keyboard
  616.               .ki.wVk = Convert.ToInt16(CChar(key))
  617.               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
  618.           End With : Inputs.Add(CurrentInput)
  619.  
  620.           ' Release the character key.
  621.           With CurrentInput
  622.               .type = NativeMethods.InputType.Keyboard
  623.               .ki.wVk = Convert.ToInt16(CChar(key))
  624.               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
  625.           End With : Inputs.Add(CurrentInput)
  626.  
  627.           ' Release the Shift key.
  628.           With CurrentInput
  629.               .type = NativeMethods.InputType.Keyboard
  630.               .ki.wVk = NativeMethods.VirtualKeys.SHIFT
  631.               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
  632.           End With : Inputs.Add(CurrentInput)
  633.  
  634.       End If ' UpperCase And My.Computer.Keyboard.CapsLock is...
  635.  
  636.       ' Send the input key.
  637.       Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
  638.                                      Marshal.SizeOf(GetType(NativeMethods.Input)))
  639.  
  640.       ' Unblock Keyboard and mouse.
  641.       If BlockInput Then NativeMethods.BlockInput(False)
  642.  
  643.   End Function
  644.  
  645.   ''' <summary>
  646.   ''' Sends a keystroke.
  647.   ''' </summary>
  648.   ''' <param name="key">
  649.   ''' Indicates the keystroke to simulate.
  650.   ''' </param>
  651.   ''' <param name="BlockInput">
  652.   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
  653.   ''' </param>
  654.   ''' <returns>
  655.   ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
  656.   ''' If the function returns zero, the input was already blocked by another thread.
  657.   ''' </returns>
  658.   Public Shared Function SendKey(ByVal key As Keys,
  659.                                  Optional BlockInput As Boolean = False) As Integer
  660.  
  661.       Return SendKey(Convert.ToChar(key), BlockInput)
  662.  
  663.   End Function
  664.  
  665.   ''' <summary>
  666.   ''' Sends a string.
  667.   ''' </summary>
  668.   ''' <param name="String">
  669.   ''' Indicates the string to send.
  670.   ''' </param>
  671.   ''' <param name="BlockInput">
  672.   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
  673.   ''' </param>
  674.   ''' <returns>
  675.   ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
  676.   ''' If the function returns zero, the input was already blocked by another thread.
  677.   ''' </returns>
  678.   Public Shared Function SendKeys(ByVal [String] As String,
  679.                                   Optional BlockInput As Boolean = False) As Integer
  680.  
  681.       Dim SuccessCount As Integer = 0
  682.  
  683.       ' Block Keyboard and mouse.
  684.       If BlockInput Then NativeMethods.BlockInput(True)
  685.  
  686.       For Each c As Char In [String]
  687.           SuccessCount += SendKey(c, BlockInput:=False)
  688.       Next c
  689.  
  690.       ' Unblock Keyboard and mouse.
  691.       If BlockInput Then NativeMethods.BlockInput(False)
  692.  
  693.       Return SuccessCount
  694.  
  695.   End Function
  696.  
  697.   ''' <summary>
  698.   ''' Slices the mouse position.
  699.   ''' </summary>
  700.   ''' <param name="Offset">
  701.   ''' Indicates the offset, in coordinates.
  702.   ''' </param>
  703.   ''' <param name="BlockInput">
  704.   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  705.   ''' </param>
  706.   ''' <returns>
  707.   ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  708.   ''' If the function returns zero, the input was already blocked by another thread.
  709.   ''' </returns>
  710.   Public Shared Function MouseMove(ByVal Offset As Point,
  711.                                    Optional BlockInput As Boolean = False) As Integer
  712.  
  713.       ' Block Keyboard and mouse.
  714.       If BlockInput Then NativeMethods.BlockInput(True)
  715.  
  716.       ' The inputs structures to send.
  717.       Dim Inputs As New List(Of NativeMethods.Input)
  718.  
  719.       ' The current input to add into the Inputs list.
  720.       Dim CurrentInput As New NativeMethods.Input
  721.  
  722.       ' Add a mouse movement.
  723.       With CurrentInput
  724.           .type = NativeMethods.InputType.Mouse
  725.           .mi.dx = Offset.X
  726.           .mi.dy = Offset.Y
  727.           .mi.dwFlags = NativeMethods.MouseInput_Flags.Move
  728.       End With : Inputs.Add(CurrentInput)
  729.  
  730.       ' Send the mouse movement.
  731.       Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
  732.                                      Marshal.SizeOf(GetType(NativeMethods.Input)))
  733.  
  734.       ' Unblock Keyboard and mouse.
  735.       If BlockInput Then NativeMethods.BlockInput(False)
  736.  
  737.   End Function
  738.  
  739.   ''' <summary>
  740.   ''' Slices the mouse position.
  741.   ''' </summary>
  742.   ''' <param name="X">
  743.   ''' Indicates the 'X' offset.
  744.   ''' </param>
  745.   ''' <param name="Y">
  746.   ''' Indicates the 'Y' offset.
  747.   ''' </param>
  748.   ''' <param name="BlockInput">
  749.   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  750.   ''' </param>
  751.   ''' <returns>
  752.   ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  753.   ''' If the function returns zero, the input was already blocked by another thread.
  754.   ''' </returns>
  755.   Public Shared Function MouseMove(ByVal X As Integer, ByVal Y As Integer,
  756.                                    Optional BlockInput As Boolean = False) As Integer
  757.  
  758.       Return MouseMove(New Point(X, Y), BlockInput)
  759.  
  760.   End Function
  761.  
  762.   ''' <summary>
  763.   ''' Moves the mouse hotspot to an absolute position, in coordinates.
  764.   ''' </summary>
  765.   ''' <param name="Position">
  766.   ''' Indicates the absolute position.
  767.   ''' </param>
  768.   ''' <param name="BlockInput">
  769.   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  770.   ''' </param>
  771.   ''' <returns>
  772.   ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  773.   ''' If the function returns zero, the input was already blocked by another thread.
  774.   ''' </returns>
  775.   Public Shared Function MousePosition(ByVal Position As Point,
  776.                                        Optional BlockInput As Boolean = False) As Integer
  777.  
  778.       ' Block Keyboard and mouse.
  779.       If BlockInput Then NativeMethods.BlockInput(True)
  780.  
  781.       ' The inputs structures to send.
  782.       Dim Inputs As New List(Of NativeMethods.Input)
  783.  
  784.       ' The current input to add into the Inputs list.
  785.       Dim CurrentInput As New NativeMethods.Input
  786.  
  787.       ' Transform the coordinates.
  788.       Position.X = CInt(Position.X * 65535 / (Screen.PrimaryScreen.Bounds.Width - 1))
  789.       Position.Y = CInt(Position.Y * 65535 / (Screen.PrimaryScreen.Bounds.Height - 1))
  790.  
  791.       ' Add an absolute mouse movement.
  792.       With CurrentInput
  793.           .type = NativeMethods.InputType.Mouse
  794.           .mi.dx = Position.X
  795.           .mi.dy = Position.Y
  796.           .mi.dwFlags = NativeMethods.MouseInput_Flags.Absolute Or NativeMethods.MouseInput_Flags.Move
  797.           .mi.time = 0
  798.       End With : Inputs.Add(CurrentInput)
  799.  
  800.       ' Send the absolute mouse movement.
  801.       Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
  802.                                      Marshal.SizeOf(GetType(NativeMethods.Input)))
  803.  
  804.       ' Unblock Keyboard and mouse.
  805.       If BlockInput Then NativeMethods.BlockInput(False)
  806.  
  807.   End Function
  808.  
  809.   ''' <summary>
  810.   ''' Moves the mouse hotspot to an absolute position, in coordinates.
  811.   ''' </summary>
  812.   ''' <param name="X">
  813.   ''' Indicates the absolute 'X' coordinate.
  814.   ''' </param>
  815.   ''' <param name="Y">
  816.   ''' Indicates the absolute 'Y' coordinate.
  817.   ''' </param>
  818.   ''' <param name="BlockInput">
  819.   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  820.   ''' </param>
  821.   ''' <returns>
  822.   ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  823.   ''' If the function returns zero, the input was already blocked by another thread.
  824.   ''' </returns>
  825.   Public Shared Function MousePosition(ByVal X As Integer, ByVal Y As Integer,
  826.                                        Optional BlockInput As Boolean = False) As Integer
  827.  
  828.       Return MousePosition(New Point(X, Y), BlockInput)
  829.  
  830.   End Function
  831.  
  832.   ''' <summary>
  833.   ''' Simulates a mouse click.
  834.   ''' </summary>
  835.   ''' <param name="MouseAction">
  836.   ''' Indicates the mouse action to perform.
  837.   ''' </param>
  838.   ''' <param name="BlockInput">
  839.   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  840.   ''' </param>
  841.   ''' <returns>
  842.   ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  843.   ''' If the function returns zero, the input was already blocked by another thread.
  844.   ''' </returns>
  845.   Public Shared Function MouseClick(ByVal MouseAction As MouseButton,
  846.                                     Optional BlockInput As Boolean = False) As Integer
  847.  
  848.       ' Block Keyboard and mouse.
  849.       If BlockInput Then NativeMethods.BlockInput(True)
  850.  
  851.       ' The inputs structures to send.
  852.       Dim Inputs As New List(Of NativeMethods.Input)
  853.  
  854.       ' The current input to add into the Inputs list.
  855.       Dim CurrentInput As New NativeMethods.Input
  856.  
  857.       ' The mouse actions to perform.
  858.       Dim MouseActions As New List(Of MouseButton)
  859.  
  860.       Select Case MouseAction
  861.  
  862.           Case MouseButton.LeftPress ' Left button, hold and release.
  863.               MouseActions.Add(MouseButton.LeftDown)
  864.               MouseActions.Add(MouseButton.LeftUp)
  865.  
  866.           Case MouseButton.RightPress ' Right button, hold and release.
  867.               MouseActions.Add(MouseButton.RightDown)
  868.               MouseActions.Add(MouseButton.RightUp)
  869.  
  870.           Case MouseButton.MiddlePress ' Middle button, hold and release.
  871.               MouseActions.Add(MouseButton.MiddleDown)
  872.               MouseActions.Add(MouseButton.MiddleUp)
  873.  
  874.           Case Else ' Other
  875.               MouseActions.Add(MouseAction)
  876.  
  877.       End Select ' MouseAction
  878.  
  879.       For Each Action As MouseButton In MouseActions
  880.  
  881.           ' Add the mouse click.
  882.           With CurrentInput
  883.               .type = NativeMethods.InputType.Mouse
  884.               '.mi.dx = Offset.X
  885.               '.mi.dy = Offset.Y
  886.               .mi.dwFlags = Action
  887.           End With : Inputs.Add(CurrentInput)
  888.  
  889.       Next Action
  890.  
  891.       ' Send the mouse click.
  892.       Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
  893.                                      Marshal.SizeOf(GetType(NativeMethods.Input)))
  894.  
  895.       ' Unblock Keyboard and mouse.
  896.       If BlockInput Then NativeMethods.BlockInput(False)
  897.  
  898.   End Function
  899.  
  900. #End Region
  901.  
  902. End Class

Saludos.


« Última modificación: 25 Abril 2014, 08:24 am por Eleкtro » En línea



El Benjo


Desconectado Desconectado

Mensajes: 392



Ver Perfil WWW
Re: sendkeys() con teclas especiales
« Respuesta #2 en: 25 Abril 2014, 18:35 pm »

Muchas gracias, Elektro. Curiosamente hace algunas horas me enteré del método SendInput() al buscar lo opuesto al método GetAsyncKeyState() dentro de la API de Windows.

Saludos.
En línea

www.es.neftis-ai.com

Sí hay un mejor lenguaje de programación y es ese con el que puedes desarrollar tus objetivos.
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Sendkeys
Programación Visual Basic
ViCrack€r 7 4,286 Último mensaje 20 Agosto 2005, 22:58 pm
por ViCrack€r
SendKeys
Programación Visual Basic
Lambda 2 2,128 Último mensaje 17 Abril 2006, 19:45 pm
por Robokop
[Solucionado] [Windows 7] [Atajo te] [Mute] ¿Como hacerlo sin teclas especiales?
Windows
Di~OsK 6 9,823 Último mensaje 31 Mayo 2011, 06:53 am
por Randomize
Manejo de teclas especiales en Linux. C.
Programación C/C++
n-utz 1 1,830 Último mensaje 18 Abril 2017, 01:02 am
por do-while
teclas especiales
Dudas Generales
Luck_Dement 3 3,047 Último mensaje 1 Febrero 2018, 12:25 pm
por Machacador
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines