ClubPanda

Can someone please help me on this...

I'm using microsoft visual basic 2005 express edition and im wondering how to auto-press a key when you press a button

e.i. i have a button in form1.vb ... i have the code written in ... Private Sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

then end sub ... im wondering how the button would make a key on the keyboard, or the mouse button in a sense auto-click by itself ... i mean, is it possible if it is can someone please tell me what the code is or explain it to me .........

p.s. tell me if its not possible

THANK YOU IN ADVANCE!!!




Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

ReneeC

sendkeys




Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

ClubPanda

what can u tell me code or something or can u explain to me i dont understand ...






Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

ReneeC

The Help facility is quite comprehensive. For example:

Windows Forms provides several options for programmatically simulating mouse and keyboard input. This topic provides an overview of these options.

Simulating Mouse Input

The best way to simulate mouse events is to call the OnEventName method that raises the mouse event you want to simulate. This option is usually possible only within custom controls and forms, because the methods that raise events are protected and cannot be accessed outside the control or form. For example, the following steps illustrate how to simulate clicking the right mouse button in code.

To programmatically click the right mouse button

  1. Create a MouseEventArgs whose Button property is set to the System.Windows.Forms.MouseButtons.Right value.

  2. Call the OnMouseClick method with this MouseEventArgs as the argument.

For more information on custom controls, see Developing Windows Forms Controls at Design Time.

There are other ways to simulate mouse input. For example, you can programmatically set a control property that represents a state that is typically set through mouse input (such as the Checked property of the CheckBox control), or you can directly call the delegate that is attached to the event you want to simulate.

Simulating Keyboard Input

Although you can simulate keyboard input by using the strategies discussed above for mouse input, Windows Forms also provides the SendKeys class for sending keystrokes to the active application.

Caution

If your application is intended for international use with a variety of keyboards, the use of System.Windows.Forms.SendKeys.Send(System.String) could yield unpredictable results and should be avoided.

To send a keystroke to the same application

  • Call the Send or SendWait method of the SendKeys class. The specified keystrokes will be received by the active control of the application. The following code example uses Send to simulate pressing the ENTER key when the user double-clicks the surface of the form. This example assumes a Form with a single Button control that has a tab index of 0.

    Visual Basic Copy Code
    ' Send a key to the button when the user double-clicks anywhere 
    ' on the form.
    Private Sub Form1_DoubleClick(ByVal sender As Object, _
      ByVal e As EventArgs) Handles Me.DoubleClick
    
      ' Send the enter key to the button, which raises the click 
      ' event for the button. This works because the tab stop of 
      ' the button is 0.
      SendKeys.Send("{ENTER}")
    End Sub
    
    C# Copy Code
    // Send a key to the button when the user double-clicks anywhere 
    // on the form.
    private void Form1_DoubleClick(object sender, EventArgs e)
    {
      // Send the enter key to the button, which raises the click 
      // event for the button. This works because the tab stop of 
      // the button is 0.
      SendKeys.Send("{ENTER}");
    }
    

To send a keystroke to a different application

  • Activate the application window that will receive the keystrokes, and then call the Send or SendWait method. Because there is no managed method to activate another application, you must use native Windows methods to force focus on other applications. The following code example uses platform invoke to call the FindWindow and SetForegroundWindow methods to activate the Calculator application window, and then calls SendWait to issue a series of calculations to the Calculator application.

    Visual Basic Copy Code
    ' Get a handle to an application window.
    Declare Auto Function FindWindow Lib "USER32.DLL" ( _
      ByVal lpClassName As String, _
      ByVal lpWindowName As String) As IntPtr
    
    ' Activate an application window.
    Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
      (ByVal hWnd As IntPtr) As Boolean
    
    ' Send a series of key presses to the Calculator application.
    Private Sub button1_Click(ByVal sender As Object, _
      ByVal e As EventArgs) Handles button1.Click
    
      ' Get a handle to the Calculator application. The window class
      ' and window name were obtained using the Spy++ tool.
      Dim calculatorHandle As IntPtr = FindWindow("SciCalc", "Calculator")
    
      ' Verify that Calculator is a running process.
      If calculatorHandle = IntPtr.Zero Then
        MsgBox("Calculator is not running.")
        Return
      End If
    
      ' Make Calculator the foreground application and send it 
      ' a set of calculations.
      SetForegroundWindow(calculatorHandle)
      SendKeys.SendWait("111")
      SendKeys.SendWait("*")
      SendKeys.SendWait("11")
      SendKeys.SendWait("=")
    End Sub
    
    C# Copy Code
    // Get a handle to an application window.
    [DllImport("USER32.DLL")]
    public static extern IntPtr FindWindow(string lpClassName,
      string lpWindowName);
    
    // Activate an application window.
    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    
    // Send a series of key presses to the Calculator application.
    private void button1_Click(object sender, EventArgs e)
    {
      // Get a handle to the Calculator application. The window class
      // and window name were obtained using the Spy++ tool.
      IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator");
    
      // Verify that Calculator is a running process.
      if (calculatorHandle == IntPtr.Zero)
      {
        MessageBox.Show("Calculator is not running.");
        return;
      }
    
      // Make Calculator the foreground application and send it 
      // a set of calculations.
      SetForegroundWindow(calculatorHandle);
      SendKeys.SendWait("111");
      SendKeys.SendWait("*");
      SendKeys.SendWait("11");
      SendKeys.SendWait("=");
    }
    

Example

The following code example is the complete application for the previous code examples.

Visual Basic Copy Code
Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Windows.Forms

Namespace SimulateKeyPress

  Class Form1
    Inherits Form
    Private WithEvents button1 As New Button()

    <STAThread()> _
    Public Shared Sub Main()
      Application.EnableVisualStyles()
      Application.Run(New Form1())
    End Sub

    Public Sub New()
      button1.Location = New Point(10, 10)
      button1.TabIndex = 0
      button1.Text = "Click to automate Calculator"
      button1.AutoSize = True
      Me.Controls.Add(button1)
    End Sub

    ' Get a handle to an application window.
    Declare Auto Function FindWindow Lib "USER32.DLL" ( _
      ByVal lpClassName As String, _
      ByVal lpWindowName As String) As IntPtr

    ' Activate an application window.
    Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
      (ByVal hWnd As IntPtr) As Boolean

    ' Send a series of key presses to the Calculator application.
    Private Sub button1_Click(ByVal sender As Object, _
      ByVal e As EventArgs) Handles button1.Click

      ' Get a handle to the Calculator application. The window class
      ' and window name were obtained using the Spy++ tool.
      Dim calculatorHandle As IntPtr = FindWindow("SciCalc", "Calculator")

      ' Verify that Calculator is a running process.
      If calculatorHandle = IntPtr.Zero Then
        MsgBox("Calculator is not running.")
        Return
      End If

      ' Make Calculator the foreground application and send it 
      ' a set of calculations.
      SetForegroundWindow(calculatorHandle)
      SendKeys.SendWait("111")
      SendKeys.SendWait("*")
      SendKeys.SendWait("11")
      SendKeys.SendWait("=")
    End Sub

    ' Send a key to the button when the user double-clicks anywhere 
    ' on the form.
    Private Sub Form1_DoubleClick(ByVal sender As Object, _
      ByVal e As EventArgs) Handles Me.DoubleClick

      ' Send the enter key to the button, which raises the click 
      ' event for the button. This works because the tab stop of 
      ' the button is 0.
      SendKeys.Send("{ENTER}")
    End Sub

  End Class
End Namespace
C# Copy Code
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace SimulateKeyPress
{
  class Form1 : Form
  {
    private Button button1 = new Button();

    [STAThread]
    public static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1()
    {
      button1.Location = new Point(10, 10);
      button1.TabIndex = 0;
      button1.Text = "Click to automate Calculator";
      button1.AutoSize = true;
      button1.Click += new EventHandler(button1_Click);

      this.DoubleClick += new EventHandler(Form1_DoubleClick);
      this.Controls.Add(button1);
    }

    // Get a handle to an application window.
    [DllImport("USER32.DLL")]
    public static extern IntPtr FindWindow(string lpClassName,
      string lpWindowName);

    // Activate an application window.
    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    // Send a series of key presses to the Calculator application.
    private void button1_Click(object sender, EventArgs e)
    {
      // Get a handle to the Calculator application. The window class
      // and window name were obtained using the Spy++ tool.
      IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator");

      // Verify that Calculator is a running process.
      if (calculatorHandle == IntPtr.Zero)
      {
        MessageBox.Show("Calculator is not running.");
        return;
      }

      // Make Calculator the foreground application and send it 
      // a set of calculations.
      SetForegroundWindow(calculatorHandle);
      SendKeys.SendWait("111");
      SendKeys.SendWait("*");
      SendKeys.SendWait("11");
      SendKeys.SendWait("=");
    }

    // Send a key to the button when the user double-clicks anywhere 
    // on the form.
    private void Form1_DoubleClick(object sender, EventArgs e)
    {
      // Send the enter key to the button, which raises the click 
      // event for the button. This works because the tab stop of 
      // the button is 0.
      SendKeys.Send("{ENTER}");
    }
  }
}

Compiling the Code

This example requires:

  • References to the System, Sys






Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

ClubPanda

ummmm ... kk 0.0






Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

DJSTU

ClubPanda, when you said you wanted to perform a button click, did you mean a visual click or just run the code event in the button





Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

ClubPanda

i wanted to ... well say i have a button in form1.vb ... when i click it i want the left mouse button to click auto-matically simoultaniously ... without me touching anything on the keyboard ... is that possible






Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

ReneeC

You can do that with Button.PerformClick

But I question the design where one button automatically depresses another. What are you trying to accomplish






Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

ClubPanda

i was trying to run something like the left mouse button auto-matically clicks like infinitively until i press stop or something






Re: Visual Basic Express Edition HOW DO YOU DO THIS ????????

JAM0

Hello ClubPanda,

I have had the same problem as you. I've wanted to make a program that "clicks" the mouse X number of times at a certian location. I did alot of searching but found a thread on the MSDN Forums that had the ansewer. Here is the sample code that it had. I've tried it a long time ago so I don't know if this code needed modified.

Code Block

Declare Auto Function mouse_event Lib "user32.dll" (ByVal dwflags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer) As Integer

Const MOUSEEVENTF_LEFTDOWN as Integer = 2
Const MOUSEEVENTF_LEFTUP as Integer = 4
Const MOUSEEVENTF_RIGHTDOWN as Integer = 8
Const MOUSEEVENTF_RIGHTUP as Integer = 16

Public Shared Sub MouseClick()
Dim x As Integer = 100
Dim y As Integer = 100;

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)

End Sub

I hope it works for you. The "Const" declarations might need to be changed to "constant" if that doesn't work "dim" always will work, but it won't be a constant variable.

Here is the thread that I got this code from http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=393261&SiteId=1. I modified the code a little because it was a cross between C# and Visual basic