PDA

View Full Version : حرفه ای: تفاوت رویداد های کنترل دکمه در 4 رویداد PreviewKeyDown و KeyPress و KeyDown و KeyUp



habibb
یک شنبه 01 دی 1392, 21:57 عصر
سلام.

اگر دکمه ای بر روی فرم قرار دهیم و بخواهیم تفاوت اجرای این 4 رویداد ( PreviewKeyDown و KeyPress و KeyDown و KeyUp) را در هنگام زدن دکمه های صفحه کلید با هم مقایسه کنیم .
به این نتایج می رسیم↓

فقط دکمه های زیر رویداد KeyPress را اجرا می کنند
ولی بقیه ی رویداد ها را اجرا نمی کنند!؟!:متعجب:چرا؟:متعجب:
دکمه ها عبارتند از : 1 2 3 4 5 6 7 8 9 0 (اعداد بالای حروف نه قسمت ماشین حسابی) و فاصلهSpace
و q w e r t y u i o p [ ] ` a s d f g h j k l ; ' z x c v b n m , . / \

حالا دکمه هایی که چند رویداد رو با هم به ترتیب زیر اجرا می کنند بازم چرا؟:متعجب:
به ترتیب عبارتند از : PreviewKeyDown و KeyPress و KeyDown

این دکمه ها دکمه های قسمت ماشین حسابی صفحه کلید می باشند بجز Enter همچنین BackSpace هم هست!

حالا دکمه هایی هستند که به ترتیب PreviewKeyDown و KeyDown را اجرا می کنند؟ بازم چرا؟:متعجب:
عبارتند از : Shift Control Alt Capslock Delete End PageUp PageDown Home End و دکمه های سوئیچ F ها

دکمه هایی هم هستند که فقط رویداد PreviewKeyDown را اجرا می کنند
که عبارتند از : هر دو Enter موجود در صفحه کلید و دکمه های ArrowKeys و Escape

حالا سوال تناقض در تعریف این رویداد ها ست؟ نظرتان چیست ؟ آیا این رویداد ها در تمام ویندوز ها این گونه هستند؟:متفکر:

طبق تعریف ؟ اگر کلیدی فشرده شود باید این 4 رویداد همه اتفاق افتند؟:متفکر:
در ضمن منابعی ذکر کنید در این مورد

متشکرم


private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
MessageBox.Show("button1_PreviewKeyDown");
}
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("button1_KeyPress");
}

private void button1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("button1_KeyDown");
}

private void button1_KeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("button1_KeyUp");
}


http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).as px

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.1 10).aspx

abbas.oveissi
یک شنبه 01 دی 1392, 22:15 عصر
چه نکات جالبی :لبخند: من در مورد KeyPress تحقیق کردم و به این نکته رسیدم ( منبع هم MSDN هست ) :
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.یعنی تناقض نیست،ماها پیگیر نبودیم که خیلی کامل داکیومنت رویدادها رو مطالعه کنیم.
اینم هم یه نکته در مورد PreviewKeyDown که برای من جالب بود،اصن نمیدونستم همچین Eventایی وجود داره :لبخند:
Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. For example, by default, a Button control ignores the arrow keys. Pressing the arrow keys typically causes the focus to move to the previous or next control. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown event for a Button. However, pressing the arrow keys for a Button does raise the PreviewKeyDown event.فکرکنم خیلی از تناقض ها حل شد :متفکر:

habibb
یک شنبه 01 دی 1392, 23:52 عصر
چه نکات جالبی :لبخند: من در مورد KeyPress تحقیق کردم و به این نکته رسیدم ( منبع هم MSDN هست ) :
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.یعنی تناقض نیست،ماها پیگیر نبودیم که خیلی کامل داکیومنت رویدادها رو مطالعه کنیم.
اینم هم یه نکته در مورد PreviewKeyDown که برای من جالب بود،اصن نمیدونستم همچین Eventایی وجود داره :لبخند:
Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. For example, by default, a Button control ignores the arrow keys. Pressing the arrow keys typically causes the focus to move to the previous or next control. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown event for a Button. However, pressing the arrow keys for a Button does raise the PreviewKeyDown event.فکرکنم خیلی از تناقض ها حل شد :متفکر:

بازم اینکه چرا این جوری کردن رو هنوز نفهمیدم؟

abbas.oveissi
دوشنبه 02 دی 1392, 00:10 صبح
خب احتمالا برای این بوده سرعت اجرای برنامه هارو بالا ببرن.بالاخره هرجی تعداد رویدادها بیشتر باشه سرعت پایین میاد