Author Topic: Virtual key  (Read 4026 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Virtual key
« on: August 09, 2006, 12:34:13 PM »
I had nothing better to do, so I tried to duplicate Keith's Capsmanager program in C#.  I cant get it to work.  Any ideas?
Code: [Select]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
namespace Caps
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label IDWindowLabel;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.IDWindowLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(208, 48);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// IDWindowLabel
//
this.IDWindowLabel.Location = new System.Drawing.Point(32, 152);
this.IDWindowLabel.Name = "IDWindowLabel";
this.IDWindowLabel.Size = new System.Drawing.Size(232, 23);
this.IDWindowLabel.TabIndex = 1;
this.IDWindowLabel.Text = "label2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.IDWindowLabel);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());

}

[ DllImport("user32.dll") ]
static extern int GetForegroundWindow();

[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);

[DllImport("user32.dll")]
public static extern short GetKeyboardState(byte[] kbBytes);

[DllImport("user32.dll")]
public static extern short SetKeyboardState(byte[] kbBytes);

private void GetActiveWindow()
{

const int nChars = 256;
int handle = 0;
StringBuilder Buff = new StringBuilder(nChars);
   
handle = GetForegroundWindow();

if ( GetWindowText(handle, Buff, nChars) > 0 )
{
this.label1.Text = Buff.ToString();
if (this.label1.Text.StartsWith("AutoCAD 2007"))
{
SetCapsLock(true);
}
else
{
SetCapsLock(false);
}
//this.label1.Text = Buff.ToString();
this.IDWindowLabel.Text = handle.ToString();
}

}

private void SetCapsLock(bool Enabled)
{
byte[] kbBytes = new byte[256];
GetKeyboardState(kbBytes);
kbBytes[20] = 0;
if (Enabled == true)
{
kbBytes[20] = 1;
}
SetKeyboardState(kbBytes);
}

private void timer1_Tick(object sender, System.EventArgs e)
{
GetActiveWindow();
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Virtual key
« Reply #1 on: August 09, 2006, 12:38:58 PM »
I had nothing better to do

:blink blink:

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Virtual key
« Reply #2 on: August 09, 2006, 05:52:37 PM »
ok, I got it working, here is what I did
Code: [Select]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;

namespace ACAD_CapsLockUtil
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{

#region Windows Form Designer generated code

private System.ComponentModel.IContainer components;

public frmMain()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <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();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain));
this.niMain = new System.Windows.Forms.NotifyIcon(this.components);
this.niMenu = new System.Windows.Forms.ContextMenu();
this.miPauseStart = new System.Windows.Forms.MenuItem();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.lblAbout = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// niMain
//
this.niMain.ContextMenu = this.niMenu;
this.niMain.Icon = ((System.Drawing.Icon)(resources.GetObject("niMain.Icon")));
this.niMain.Text = "TEP AutoCad CapsLock Utility";
this.niMain.Visible = true;
//
// niMenu
//
this.niMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
   this.miPauseStart,
   this.menuItem1,
   this.menuItem2});
//
// miPauseStart
//
this.miPauseStart.Index = 0;
this.miPauseStart.Text = "Pause";
this.miPauseStart.Click += new System.EventHandler(this.miPauseStart_Click);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// lblAbout
//
this.lblAbout.Location = new System.Drawing.Point(48, 8);
this.lblAbout.Name = "lblAbout";
this.lblAbout.Size = new System.Drawing.Size(232, 32);
this.lblAbout.TabIndex = 0;
this.lblAbout.Text = "About";
this.lblAbout.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(8, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(32, 32);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(264, 168);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(112, 232);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 3;
this.btnOK.Text = "OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// menuItem1
//
this.menuItem1.Index = 1;
this.menuItem1.Text = "-";
//
// menuItem2
//
this.menuItem2.Index = 2;
this.menuItem2.Text = "Help";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.ControlBox = false;
this.Controls.Add(this.btnOK);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.lblAbout);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "frmMain";
this.Text = "TEP AutoCad CapsLock Utility";
this.Load += new System.EventHandler(this.frmMain_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmMain_Paint);
this.ResumeLayout(false);

}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}

private System.Windows.Forms.NotifyIcon niMain;
private System.Windows.Forms.ContextMenu niMenu;
private System.Windows.Forms.MenuItem miPauseStart;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label lblAbout;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;

#endregion

const byte VK_CAPITAL=0x14;
string ActiveAppTitle = "Rob H. Needs A Raise Without Any Added Responsibilities";
bool Initial = false;

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

[ DllImport("user32.dll") ]
static extern int GetForegroundWindow();

[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);

[DllImport("user32.dll")]
public static extern short GetKeyState(int nVirtKey);

private void ToggleKey(byte KeyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(KeyCode, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0);
keybd_event(KeyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr) 0);
}

private void miPauseStart_Click(object sender, System.EventArgs e)
{
if (timer1.Enabled)
{
timer1.Enabled = false;
miPauseStart.Text = "Re-Start";
}
else
{
timer1.Enabled = true;
miPauseStart.Text = "Pause";
}
}

private void frmMain_Load(object sender, System.EventArgs e)
{
lblAbout.Text = "This program was created for TEP \n by Robert Houts and David Hall";
label1.Text = "This program was created to ensure that the caps lock key is enabled while you are working in AutoCAD.  When AutoCAD is the active window, it will turn the Caps Lock key On and when you leave AutoCAD, it will turn Caps Lock Off.  \n\nKeep in mind, you can still manually toggle the Caps Lock key on your own and you can pause this program by right-clicking the 'C' icon and selecting 'Pause'.  You re-enable this program by right-clicking the 'C' icon and selecting 'Re-Start'. \n\nThank you and Happy Drafting! :-)";
}

private void btnOK_Click(object sender, System.EventArgs e)
{
this.Hide();
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Visible = true;
}

private void timer1_Tick(object sender, System.EventArgs e)
{
string CurrentTitle = GetActiveWindowTitle();
if (ActiveAppTitle != CurrentTitle)
{
if (CurrentTitle.StartsWith("AutoCAD "))
{
if (GetKeyState(VK_CAPITAL) == 0)
{
ToggleKey(VK_CAPITAL);
}
}
else
{
if (GetKeyState(VK_CAPITAL) == 1)
{
ToggleKey(VK_CAPITAL);
}
}
}
ActiveAppTitle = CurrentTitle;
CurrentTitle = string.Empty;
}

private string GetActiveWindowTitle()
{
const int nChars = 256;
int handle = 0;
StringBuilder Buff = new StringBuilder(nChars);   
handle = GetForegroundWindow();
if ( GetWindowText(handle, Buff, nChars) > 0 )
return Buff.ToString();
else
return string.Empty;
}

private void frmMain_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (Initial == false)
{
this.Hide();
Initial = true;
}
}
}
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Virtual key
« Reply #3 on: August 09, 2006, 06:36:53 PM »
Nice one 'Duh, there is proabably something there to help Dave Baranas in all that for his palette problem. I found the same trouble but didn't want to dip into PInvoke to hook into the keyboard to fix it but it may be the only way.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Virtual key
« Reply #4 on: August 09, 2006, 10:12:59 PM »
OK, so whatever is typed, the displayed text is "David and Rob deserve a substantial raise" ... yes ?

[seriously]
Until we all come to grips with theparticular idiosyncrasies of the language, or till "someone" produces docs, whatever works may be the way to go ..   Not purist, I know, but bloody practical !!
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.