Solved

jQuery's event object's metakey property not working

Posted on 2015-01-13
3
191 Views
Last Modified: 2015-01-16
I was trying out a sample code, and I noticed the metakey property of the event object is not working as it keeps reporting back false even when I press the ctrl key in Windows 7. Can anyone please tell me why? I checked jQuery's bug tracker, they say it has been fixed, but not from my point of view, especially since it doesn't work on any browser I tested this with (IE, Firefox, Opera, Chrome, and Safari).

<!DOCTYPE html>
<html>
<head>
    <title>jQuery Event Handling</title>
    <link rel="stylesheet" href="../style.css"/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript">
        $("document").ready(function() {
            $("#example").on("mousemove", onMouseOver);
            $("#example").on("click", onMouseClick);
            $("#example").on("mouseleave", onMouseLeave);
        });
        
        function onMouseOver(evt) {
            $("#example").text(evt.type + ": " + evt.pageX + ", " + evt.pageY
                               + "\n" + "Button: " + evt.which + 
                               " Key: " + evt.metaKey);
        }
        function onMouseClick(evt) {
            $("#example").text(evt.type + ": " + evt.pageX + ", " + evt.pageY);
            $("#example").off("mousemove", onMouseOver);
        }
        function onMouseLeave(evt) {
            $("#example").text("mouseleave");
        }
    </script>
</head>
<body>
    <h1>jQuery Event Handling</h1>
    <p>jQuery normalizes an event structure across browsers and provides cross-browser consistency for properties such as the event name, the page coordinates of the mouse, the element where the event originated along with any other element related to the event, and information such as whether a meta-key or specific mouse button was pressed.</p>
    <div id="example">
    </div>
</body>
</html>

Open in new window

0
Comment
Question by:elepil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 40554442
The ctrl key isn't classed as a meta key. The Windows key is the only meta key on standard keyboards and it seems that key isn't exposed to the browser to capture.
0
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 40554446
see here for a demo using the ctrl key: http://jsbin.com/piyuwu/1/edit?html,js

I've also changed your events to be captured over the <body>, not the empty "example" div
0
 

Author Closing Comment

by:elepil
ID: 40554795
Thanks, Rob, that answers my question!
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question