C # Basic Video tutorial 5.1 How to write a simple super hotkey

Source: Internet
Author: User

Our last section described writing a simple calculator, which is actually a practical point of view (this calculator will certainly work better than Microsoft's own calculator as long as you're willing to change it). This section introduces the simple Super Hotkey (the so-called Super Hotkey refers to the function of the custom shortcut key)

Super Hotkey is the most critical step is to bind the keyboard hook, is the keyboard press a set of shortcut keys, the main form can respond to (do not do or think how to do, Baidu search others have no mature can learn from), first look at other people's sample program (understand what is the project with class library output, Right click can be modified to demo test run, directly run the error)

?

Also learn to be able to generate class libraries and use class libraries, such as our project to create a new class library that looks no different than a traditional C # project

?

Take the previous section we wrote Functionmath This class direct copy replaces the default content, note that System.Windows.Forms is referenced, and you want to right-click the Add reference in the project reference

?

Click Build, rebuild the solution, and you can see that the corresponding class library DLL file is generated in the Debug directory (you can also change the name)

?

So how do you use your own library of classes in your current project? Copy this DLL into your new project's debug directory, then right-click on the current project to add a reference to this DLL

?

The new project can then add references to the previous DLLs and use our own defined properties and methods.

?

Let's take a look at the contrast, when we define the method of Functionmath, we add a prefix called static, which is static meaning. In order to compare, we then copy the original class base, remove the static modifier, and regenerate the DLL.

?

This DLL overwrite the previous (note that after writing code also as long as this, the update of a class library directly overwrite the original, it is equivalent to update the module code, which is the C # more efficient place, the new code is more than the old, so the generated DLL is a little larger than before)

?

We found that this can be automated code hint FUNCTIONMATH2, but could not find any of his methods, because this class is not static (static class does not need to declare an instance of the class, the direct class name .) Method Name )

?

So how do you use the FunctionMath2 method? First an instance of the new class (a non-static class needs to declare an instance of the class, and then the instance name .) method name ). So from the present angle of view, seemingly static more convenient, want to use at any time, do not need new instance, if you code thousands of tens of thousands of lines, you may not remember so many instances of the class name. However, in some cases, it is more meaningful to declare an instance of the class with new (for example, when explaining the communication, you may need 10 instances of communication at the same time to run, each instance of the port, the connecting client is not the same, then this time should be divided clearly, like you have 10 sales, Although each sales responsibility is the same, but each sales responsible customer is different, do you think that all sales tasks are left to a person to complete the appropriate or separate the appropriate? )

?

Add a bit here, when you modify the code, to use some third-party editors, such as UltraEdit (also can use other such as notepad++), these third-party editors than Notepad or word is much more useful, there are display line numbers, code highlighting and so on. To test the static, we can replace the static with empty (note that the static+ space is replaced, otherwise there will be two spaces between the public and the double), using a third-party editor can significantly improve programming efficiency.

?

Understanding the role of static, we will then understand the role of private. If we change the prefix of a method from public to private, then we cannot access this method when we use it, because this method is not publicly available (private = "privately"), then why should we have this private method? For example, you write a very complex class library, there are a lot of intermediate operations, a method call B,b call C, but this kind of library needs to be kept clean (such as mathematical methods, no matter how complex inside, external is the basic operation of subtraction, as to how you implement the internal, or you do not want to let others access, Get more and make people feel messy, wrong to say you have this problem).

?

It is important to note that, in addition to the method can distinguish public/private, whether there is static, the class itself, as well as the properties of the class can be added such modifiers. For example, I can define a global variable of pi in Functionmath (a custom precision of 3.1415, so all the following methods can use this pi uniformly)

?

So if you add static properties to the class itself, all of the following properties and methods are required to be static (this is generally not necessary)

?

With these knowledge of classes and DLLs, we immediately put the class library of people's threads into their own programs. For example or this program, we checked the detection KeyDown, you can detect the keyboard action in the text box below (other mouse actions can also be detected, you can test yourself)

?

View the specific code that is executed after the control is ticked

?

Again, in order to know what this m_keyboardhookmanager is, you can right-click on the object and go to the definition (the result location shows that he is an instance of the Keyboardhooklistener class, That is, all the events, attributes, and methods that he owns Keyboardhooklistener.

?

So how many properties and methods does Keyboardhooklistener have? We just know about the KeyDown event, and then we're interested in the Keyboardhooklistener right click View definition, you can find him there are three events (keyboard press, keyboard bounce, keyboard press, seemingly keyboard can only have these three kinds of events).

?

Here we introduce a new noun event, which is a good understanding of what is going on. The most common control events, such as button click events, keyboard press events, and of course other events, such as shutdown events, QQ receives a message event, these are events. To complete a control corresponding to the event is not displayed, just this check box there are so many events, we only need to master the most common (and in this section we do not specify the event-delegate content in detail, followed by a more practical example, the event-delegation mechanism or C # A very important piece of content, the reader should be able to understand the definition of other people's events-the delegate, but also in their own class library to write such an event-delegate, this chapter we simply can use it.

?

We copy the sample code, declaring classes and instances of the class, and initializing the class in the same place (copying someone else's code is the basic skill, do not feel ashamed, copy is not like innovation)

?

The quote is over, the statement is finished, and the next part is used. We can understand that if the check box CheckBox1 is checked, then the M_keyboardhookmanager KeyDown event occurs, let the Hookmanager_keydown method execute (As for m_ Keyboardhookmanager Why can you respond to our keyboard KeyDown event, we do not need to worry about, others encapsulate the class library, where we just use), you can always right-click method to find specific what to do, here we just let a text box display just the button, Similar to the sample code.

?

To verify our functionality, it's true (readers can also learn how to transplant a part of the mouse that responds, so it's not going to go into it)

?

Note that there is more than one way to bind a keyboard hook, in the reference code of the attachment, I also give other people's other practices (such as calling the win API parameters, regardless of business logic can also directly build the combination hotkey, readers can also transplant their code)

?

?

For more instructional videos and downloads, please pay attention to the following information:

My Youku Space:

Http://i.youku.com/acetaohai123

?

My online forum:

http://csrobot.gz01.bdysite.com/

?

Problem Exchange:

qq:910358960

Email:[email protected]

?

?

?

C # Basic Video tutorial 5.1 How to write a simple super hotkey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.