0

I'm wonder if here is any way to add custom constant to system constants.

For example in my coding I often uses such code:

VB.NET
Private Sub myform_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.F3 Or (e.Control And e.KeyCode = Keys.F) Then
        search(mytextbox.Text.Trim)
    End If
End Sub

C#
private void myform_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F3 | (e.Control & e.KeyCode == Keys.F)) {
    search(mytextbox.Text.Trim);
    }
}

Idea is to add new element to Enum Keys, say "Find" So I would be able to do this:

VB.NET    
If e.KeyCode = Keys.Find Then
    search(mytextbox.Text.Trim)
End If

C#
if (e.KeyCode == Keys.Find) {
    search(mytextbox.Text.Trim);
}

Of course I would first replace keystrokes Keys.F3 and e.Control+Keys.F to Keys.Find in some lower level "ProcessCmdKey" on Applicaton level like this:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean

    If keyData = Keys.F3 Then
        msg.WParam = CType(Keys.Find, IntPtr)
        keyData = Keys.Find
    End If

    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

So, question is: Could Keys.Find be added to Keys constants and if do - how?

Wine Too
  • 4,515
  • 22
  • 83
  • 137

1 Answers1

0

No, because you don't control the source of the data (e.KeyCode). Let's say you could add a new value to the enum how could you tell windows to start sending you this particular new value when it detects the combination of control and keys?

Slugart
  • 4,535
  • 24
  • 32
  • I mention that. I will override ProcessCmdKey from keys pressed to keys wanted. – Wine Too Oct 20 '13 at 20:13
  • Do you know what ProcessCmdKeys can do? http://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx – Slugart Oct 20 '13 at 20:18
  • Of course, I use it among others for changing/replacing keys: If keyData = Keys.Enter Then keyData = Keys.Down msg.WParam = CType(Keys.Down, IntPtr) End If When I press Keys.Enter I catch e.KeyCode for Keys.Down in my form! – Wine Too Oct 20 '13 at 20:27