Js: Get the keyCode of the keyboard ------- Day42

Source: Internet
Author: User

Today, Jinan is full of rain. This is a hot and empty day, but some of them are thunderous. I originally wanted to complete today's blog record in the company, but I don't know why I couldn't log on to the blog, it is estimated that CSDN had a problem with the server at that time. Fortunately, at night, the thunder was less than enough to continue to open the computer.

Many times I think I am a strange person. If I am interested in the same thing, I can be excited and tired. But if I am not interested, I will suffer every moment, just like running, I always feel that long-distance running is a very painful thing. It takes a long time to catch up when I run 400. Some friends have invited me to take a basketball shot to run, even if I don't play, just shoot and running, it's so silly for a long time. The key is that it feels great ....

With regard to javascript learning, I always feel that I can't look at the code in a monotonous way. I still want to have some fun for myself, and the game becomes a starting point. It is even more fun to interact with each other, so for a computer, the interactive means we can achieve is really limited: the keyboard, the mouse, of course, if there is a touch screen can also be calculated, and if you want effective applications, at least you need to know what you are talking about and what commands you tell it. This requires us to have a clearer understanding of its transmission principles and syntax, of course, we all know that a machine reads numbers, so how can we get the digital commands for it.

In this way, I will first attach the write method for obtaining the keyCode of the keyboard:

Html language

<div style="width: 500px;height: 200px;background-color: cyan;" id="showZone"></div>
Javascript method:

document.onkeydown=function(event){var event=event||window.event;document.getElementById("showZone").innerHTML=event.keyCode;};
In this way, I can obtain the command after clicking a key. For example, if I press "1" and it shows "49", this is the right one.

First, let's take a look at this method. Although it is already an old acquaintance and has been used for many times, it is only today that we have made it clear.

Event: the event in the function (event), I tried to change the name, and also tried to delete it directly, it is not affected in ie, so that I can trust those statements.

In ie, there is a built-in event object. Write it more because it is compatible;

The event can also be written here. The style of the event is just for easier understanding;

Var event = event | window. event, remember, I wrote a special article before, and now I can see it again. It's just a piece of shit, but one thing is right, var event = event? Event | window. event is easier to understand.

Next, let's take a look at the corresponding buttons ,:




I have to say that the network is a good thing. These are all well organized and awesome. If we really want to have game operations, we need to pay more attention to them, after all, the power of habits is enormous.

A: 65, w: 87, s: 83, d: 68, left arrow: 37, clockwise rotation up, right, down in turn: 38, 39, 40, j: 74

Let's start with a few examples. We still need to remember more.


Maybe I am not happy, but I will leave the most beautiful marks on those roads. Come on...


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.