开发者

Windows.Form not fire keyDown event

开发者 https://www.devze.com 2023-01-08 16:54 出处:网络
private void screensaverWindow_Load(object sender, System.EventArgs e) { this.BringToFront(); this.Focus();
private void screensaverWindow_Load(object sender, System.EventArgs e)
{            
    this.BringToFront();            
    this.Focus();
    this.KeyPreview = true;
    this.KeyDown += new KeyEventHandler(onkeyDown);            
}        

onKeyDown() is never called. Any idea why?

EDIT: This works in release mode!? I guess it must be visual studio debugger interfer开发者_如何学运维ing somewhere


This is caused by the interception of events by child controls on form. If this is not desirable, you need set KeyPreview property of parent form to True


Seems to work for me:

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    this.BringToFront();
    this.Focus();
    this.KeyPreview = true;
    this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}

void Form1_KeyDown(object sender, KeyEventArgs e)
{
    Console.WriteLine("test");
}

Are there any child controls on your form ?


How about menus, do you have menus with shortcut keys defined? I had an issue where a context menu that was hidden was actually swallowing cetain keys and not passing them on to any of the form key events

0

精彩评论

暂无评论...
验证码 换一张
取 消