Jump to content

C# Key Sender

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Genny was working with Visual Basic on a Program that sends the ~ key to his BF game. He asked me to replicate his program in C# for testing so I've attached it here along with a screenshot and posted the code.

Form Design:
[highlight="C#"]
namespace SendKeys
{
partial class fmrMain
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.btnSndKey = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.timerSndKey = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// btnSndKey
//
this.btnSndKey.Location = new System.Drawing.Point(38, 34);
this.btnSndKey.Name = "btnSndKey";
this.btnSndKey.Size = new System.Drawing.Size(127, 42);
this.btnSndKey.TabIndex = 0;
this.btnSndKey.Text = "Send Key";
this.btnSndKey.UseVisualStyleBackColor = true;
this.btnSndKey.Click += new System.EventHandler(this.btnSndKey_Click);
//
// btnStop
//
this.btnStop.Enabled = false;
this.btnStop.Location = new System.Drawing.Point(171, 34);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(127, 42);
this.btnStop.TabIndex = 1;
this.btnStop.Text = "Stop";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// timerSndKey
//
this.timerSndKey.Interval = 3000;
this.timerSndKey.Tick += new System.EventHandler(this.timerSndKey_Tick);
//
// fmrMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(344, 106);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnSndKey);
this.Name = "fmrMain";
this.Text = "Send Key";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button btnSndKey;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Timer timerSndKey;
}
}
[/highlight]

frmMain.cs
[highlight="C#"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SendKeys
{
public partial class fmrMain : Form
{
public fmrMain()
{
InitializeComponent();
}

/// <summary>
/// Form loader. We have nothing to initialize upon
/// load. Blank.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{

}

/// <summary>
/// This will start a timer that then sends
/// the key ~ every 3 seconds
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSndKey_Click(object sender, EventArgs e)
{
// Disable start button, enable stop button
btnSndKey.Enabled = false;
timerSndKey.Enabled = true;
btnStop.Enabled = true;
}

/// <summary>
/// This will stop the timer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{
// Disable start button, enable stop button
btnSndKey.Enabled = true;
timerSndKey.Enabled = false;
btnStop.Enabled = false;
}

/// <summary>
/// When the timer reaches 3000 ms (3 seconds) it will
/// execute this function which simply sends a key.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerSndKey_Tick(object sender, EventArgs e)
{
System.Windows.Forms.SendKeys.Send("{~}");
}


}
}
[/highlight]

Program.cs
[highlight="C#"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace SendKeys
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new fmrMain());
}
}
}
[/highlight]


This program loads a form with two buttons. When you press the "Send Key" button every 3 seconds the Tilda (~) key will be sent to the currently focused window. Pressing stop will stop the application.

Attached Files



#2
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
keybd_event() or SendInput() for those of us who want to avoid.

hi.in.order.to.send.a.key.use.this.function()

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I figured you would like this code post. Also, if you declare use of the namespace you can reduce the code above down to:

SendKeys.Send("{~}");


#4
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Which is just a wrapper function.

#5
Genny

Genny

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Thanks alot J i'll give it a test
CC Design Team - Leader
Posted Image

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
It seems like a lot of code, but for those who are interested, the only important line is this:
[HIGHLIGHT=csharp] System.Windows.Forms.SendKeys.Send("{~}");[/HIGHLIGHT]
This sends the key to the active window.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Yes, but without the rest of the code, including the timer, the ~ will only send once and you may not have enough time to focus your application before it is sent.

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Of course! I was just saying, it seems like an overwhelming amount of code for someone who doesn't know much C#, but I was just singling out that line, because that's the line that does the work. People can then use that line to customise their own program that does whatever they want.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
Donovan

Donovan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 798 posts
looks good lol, but I wish it didn't have the line numbers so I could copy and paste to try it out and play with it a bit:P
Posted Image
+Friend Me | My Graphics | Forum Rules | Help Forum | Forum FAQ

#10
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Download the attachment - it is the full source.

#11
dieucay555

dieucay555

    Newbie

  • Members
  • Pip
  • 2 posts
Download the attachment:rolleyes:

#12
tuichuai

tuichuai

    Newbie

  • Members
  • Pip
  • 1 posts
Download the attachment - it is the full source.