Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created March 10, 2024 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/d54c39e20753ff8b11a0c84bb9ff2cf5 to your computer and use it in GitHub Desktop.
Save dj1711572002/d54c39e20753ff8b11a0c84bb9ff2cf5 to your computer and use it in GitHub Desktop.
C# KeyIn Button for tablet
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection.Emit;
namespace ButtonKeyTest00
{
public partial class Form1 : Form
{
//[DllImport("user32.dll")]
//public static extern IntPtr GetForegroundWindow();
//[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)]
//public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
bool flgClick = false;
int count = 0;
public int ap;
public string exename1="STA24_Console_BinDataToCSV_rev01";
public string exename2 = "STA24_Console_MB_SerialMon_rev00";//teratermpro
public Form1()
{
InitializeComponent();
//
button1.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button2.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button3.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button4.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button5.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button6.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button7.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button8.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button9.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button10.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button11.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button12.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button13.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button14.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button15.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button16.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
label1.Font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
button1.Text = "a";
button2.Text = "u";
button3.Text = "s";
button4.Text = "d";
button5.Text = "1";
button6.Text = "2";
button7.Text = "3";
button8.Text = "4";
button9.Text = "5";
button10.Text = "6";
button11.Text = "7";
button12.Text = "8";
button13.Text = "9";
button14.Text = "0";
button15.Text = "{Enter}";
button16.Text = "m";
radioButton1.Text = exename1;
radioButton2.Text = exename2;
if (radioButton1.Checked == true)
{
ap = 1;
}
else
{
ap = 2;
}
//System.Diagnostics.Process.Start(@C:\Program Files (x86)\teraterm\STA24_Console_BinDataToCSV_rev01.exe");
radioButton1.Checked= true;
//System.Diagnostics.Process.Start(@"C:\Program Files (x86)\teraterm\ttermpro.exe");
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(0, 0);
}
//-------------Radiobutton-------------------------------
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
ap = 1;
}
else
{
ap = 2;
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked == true)
{
ap = 2;
}
else
{
ap = 1;
}
}
//------------------Button 定義------------------------------------
private void button1_Click(object sender, EventArgs e)
{
//System.Diagnostics.Process.Start(@"C:\Users\KM\Desktop\rev01\STA24_Console_BinDataToCSV_rev01.exe");
// Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
if (ap == 1)
{
Microsoft.VisualBasic.Interaction.AppActivate(exename1);
//label1.Text = "ap=1";
}
else
{
Microsoft.VisualBasic.Interaction.AppActivate(exename2);
//label1.Text = "ap=2";
}
SendKeys.Send("a");
}
private void button2_Click(object sender, EventArgs e)
{
if (ap == 1)
{
Microsoft.VisualBasic.Interaction.AppActivate(exename1);
//label1.Text = "ap=1";
}
else
{
Microsoft.VisualBasic.Interaction.AppActivate(exename2);
//label1.Text = "ap=2";
}
SendKeys.Send("u");
label1.Text = "u";
}
private void button3_Click(object sender, EventArgs e)
{
//if (ap == 1)
// {
// Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
// }
// else
// {
Microsoft.VisualBasic.Interaction.AppActivate(exename2);
//}
SendKeys.Send("s");
label1.Text = "s";
}
private void button4_Click(object sender, EventArgs e)
{
if (ap == 1)
{
Microsoft.VisualBasic.Interaction.AppActivate(exename1);
//label1.Text = "ap=1";
}
else
{
Microsoft.VisualBasic.Interaction.AppActivate(exename2);
//label1.Text = "ap=2";
}
SendKeys.Send("d");
label1.Text = "d";
}
private void button5_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("1");
label1.Text = "1";
}
private void button6_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("2");
label1.Text = "2";
}
private void button7_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("3");
label1.Text = "3";
}
private void button8_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("4");
label1.Text = "4";
}
private void button9_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("5");
label1.Text = "5";
}
private void button10_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("6");
label1.Text = "6";
}
private void button11_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("7");
label1.Text = "7";
}
private void button12_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("8");
label1.Text = "8";
}
private void button13_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("9");
label1.Text = "9";
}
private void button14_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("0");
label1.Text = "0";
}
private void button15_Click(object sender, EventArgs e)
{
Microsoft.VisualBasic.Interaction.AppActivate("STA24_Console_BinDataToCSV_rev01");
SendKeys.Send("{ENTER}");
label1.Text = "{ENTER}";
}
private void button16_Click(object sender, EventArgs e)
{
if (ap == 1)
{
Microsoft.VisualBasic.Interaction.AppActivate(exename1);
//label1.Text = "ap=1";
}
else
{
Microsoft.VisualBasic.Interaction.AppActivate(exename2);
//label1.Text = "ap=2";
}
SendKeys.Send("m");
label1.Text = "m";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment