\

Facebook


วันอังคารที่ 1 ตุลาคม พ.ศ. 2556

[WinForm C#] Basic Keyboard Reader

Almost of any software which have builded for user's comfortable have special shortcut keys, so today i would like to present easy WinForm keys reader.

In Winform c#, which interite from base Form of Windows.system also include reading key method. just create new project call "ReadKeyBoard" and add new main class call "Form1". Then, in properties tab click on Events button to reveal all form's event inside and double click on "KeyDown" event to active usage.


Let's see inside "Form1_KeyDown" method, it accepts two parameter which one is Object and another one is KeyEventArg e. So anytime you press Keyboard and take key up, program should automatically detect on "what key you press" and send it into method with e value. For example, i press "Backspace", i should get valuse as Keys.Back.

For combination keys such as CTRL + C, SHIFT + F4, you should get 2 values while taking inside this method. One modify key and one normal key, so make sure you have check for specific case. Just like combine Ctrl + C = copy :

if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C)
            {
                // Do Copy stuff
            }

For normal key only :

 else  if (e.KeyCode == Keys.Space)
            {
                // Do your stuff
            }

Write other conditions as you want. i post the code example below. Have a nice day with less bug. Thank for interesting.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace CombineKey
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C)
            {
                textBox1.Text = "Control + C";
            }
            else if (e.KeyCode == Keys.Back)
            {
                textBox1.Text = "Back";
            }
            else  if (e.KeyCode == Keys.Space)
            {
                textBox1.Text = "Space Bar";
            }
            else if (e.KeyCode == Keys.F4)
            {
                textBox1.Text = "F4";
            }
            else if (e.KeyCode == Keys.Tab)
            {
                textBox1.Text = "Tab";
            }
            else
            {
                textBox1.Text = "None";
            }
        }
    }
}
 
<div class="preloader"><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>

ไม่มีความคิดเห็น:

May be like this posts

  • C# EventLog Viewer part II รู้หมด! ใครแอบมาใช้คอมเรา ภาคสอง
    06/05/2014 - 0 Comments
    จากที่เคยแนะนำเรื่อง ดูการใช้งานล่าสุดใน EventLog ที่เราต้องเปิด Event Viewer ขึ้นมาเพื่อ เลือกดู log…
  • SQL SERVER ERROR: 40 บันทึกการพังของเอสคิวแอลเซิฟเวอร์
    25/08/2014 - 0 Comments
    SQL SERVER ERROR : 40 - Could not open connection to SQL Server ใครเจอ error code 40 ในตัว SQL SERVER…
  • ว่างจัด! ทำเว็บดึง Facebook Graph API ง่ายๆ
    04/08/2014 - 0 Comments
    ไม่ได้เขียน BLOG มาร่วม 3 เดือนละมั้งครับเนี้ย วันนี้ขออัพเดตหน่อยละกันครับ…
  • Doodle Rubik ตัวใหม่ของ Google เล่นกันละยัง
    18/05/2014 - 0 Comments
    วันนี้ 19/5 เข้าหน้ากูเกิ้ลแล้วเจอ Doodle ตัวใหม่ เป็นรูบิค 3x3…
  • ลบ .Bak แสนกวนใจใน Edit Plus
    28/05/2013 - 0 Comments
    ลบ .Bak แสนกวนใจใน Edit Plus สำหรับใครที่ใช้ Edit plus เป็น Editor ในการเขียนโค๊ดไม่ว่าจะเป็น html php c/c++…
  • [Skype] Emoticon ใหม่จากหนังเรื่อง Captain America: the winter soldier ลองกันละยัง
    17/04/2014 - 0 Comments
    Emoticon ที่ซ่อนอยู่ของ Skype Skype จะมีอิโมติคอนที่ซ่อนอยู่ซึ่งจะไม่แสดงตรงหน้ากรอก message…
  • เจ้าเต่าควบคุม Version : tortoiseSVN
    01/04/2013 - 0 Comments
    โปรแกรมตัวนี้ก็เป็นเครื่องมือที่ใช้ควบคุมเวอร์ชั่นของโปรแกรมที่เราพัฒนา ถ้านึกภาพยังไม่ออกก็ลองนึกถึง…