Pritorian
Pritorian

Reputation: 450

How to catch ESC key press with WndProc?

How to catch an ESC KeyPress using WndProc?

Upvotes: 2

Views: 9719

Answers (4)

adjutant
adjutant

Reputation: 63

Another option (for forms):

protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m)
{
  int VK_ESCAPE = 27;
  if (m.Msg == Win32Constants.WM_KEYDOWN && (int)m.WParam == VK_ESCAPE)
  {
    // ...
  }
  return base.ProcessKeyPreview(ref m);
}

Upvotes: 5

Luca Rocchi
Luca Rocchi

Reputation: 6464

(msg==WM_KEYDOWN) && (wParam==VK_ESCAPE) ... ops it was c#... sorry , that is win32 api way

Upvotes: 0

t0mm13b
t0mm13b

Reputation: 34592

Why are you doing it this way? Why not set the PreviewKey property of the Form to true and set a global event handler for KeyUp and check it...

if (e.KeyCode == Keys.Esc){
   //...
}

Upvotes: 3

SLaks
SLaks

Reputation: 887305

You need to catch the WM_CHAR message and check WParam.

Upvotes: 0

Related Questions