TheSwamp

Code Red => .NET => Topic started by: hermanm on January 29, 2010, 09:32:47 PM

Title: Capture Keystrokes?
Post by: hermanm on January 29, 2010, 09:32:47 PM
Not really an AutoCAD question, but is it possible using .NET to intercept
keyboard input, similar to grread?
If this is possible, I would appreciate a heads-up to the appropriate
reference.
If not possible, would like to know that as well.

Thx for any assistance.

Title: Re: Capture Keystrokes?
Post by: Chuck Gabriel on January 29, 2010, 09:43:04 PM
If you are talking about capturing keystrokes sent to the AutoCAD window, take a look at the attached project.  There may be other (better) ways to do it, but this works.

If you just want to capture keystrokes from Windows forms, I'm sure there is an easier way to do it.
Title: Re: Capture Keystrokes?
Post by: sinc on January 30, 2010, 09:19:10 AM
Here's an example of a custom UserControl that captures the "Enter" key.

This creates a text box that behaves like the standard text box, but when the user has the cursor in this text box and hits "Enter", it does not trigger the form's OK button.

Code: [Select]
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Quux.AcadUtilities.Forms
{
    public class QuuxTextBox : TextBox
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (SuppressEnter)
                if (msg.Msg == 256 && keyData == Keys.Enter)
                {
                    OnRaiseEnterKeyPressedEvent(new EventArgs());
                    return true;
                }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        [Category("Behavior")]
        [Description("When set, keeps the ENTER key from triggering the form 'OK' button.")]
        public bool SuppressEnter { get; set; }

        /// <summary>
        /// Fires when 'SuppressEnter' is set to TRUE and user hits ENTER key while editing Textbox.
        /// </summary>
        [Category("Action")]
        [Description("Fires when 'SuppressEnter' is set to TRUE and user hits ENTER key while editing Textbox.")]
        public event EventHandler EnterKeyPressed;

        protected virtual void OnRaiseEnterKeyPressedEvent(EventArgs e)
        {
            EventHandler handler = EnterKeyPressed;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
}
Title: Re: Capture Keystrokes?
Post by: hermanm on January 30, 2010, 09:30:34 AM
Thank you very much for the examples.:)