{"id":2584,"date":"2017-02-27T01:06:37","date_gmt":"2017-02-27T00:06:37","guid":{"rendered":"http:\/\/codingexplained.com\/?p=2584"},"modified":"2017-06-11T21:46:24","modified_gmt":"2017-06-11T19:46:24","slug":"vue-key-modifiers","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers","title":{"rendered":"Vue Key Modifiers"},"content":{"rendered":"<p>\n<iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/ztcDhUOKW9Q\" frameborder=\"0\" allowfullscreen><\/iframe>\n<\/p>\n<p>In the previous post, we took a look at event modifiers. In this post, we will be looking at a more specialized category of event modifiers, namely so-called <em>key modifiers<\/em>.<\/p>\n<p>As the name suggests, this category of event modifiers can be used with keyboard events.<\/p>\n<p>Say that we have a text input, for example, and we want to do something if the user hits the Enter key, such as submitting a form or something like that. We could do that by using the keyup event with the <span class=\"code\">v-on<\/span> directive, and the <span class=\"code\">enter<\/span> key modifier.<\/p>\n<pre><code class=\"html\">&lt;input type=\u201dtext\u201d v-on:keyup.enter=\u201dpressedEnter\u201d&gt;<!--formatted--><\/code><\/pre>\n<p>The <span class=\"code\">enter<\/span> modifier ensures that the expression is only executed when the user releases the Enter key. I\u2019ll just quickly add a method which shows an alert, just so that it is easy to test.<\/p>\n<pre><code class=\"javascript\">new Vue({\r\n\tel: &#039;#app&#039;,\r\n\tmethods: {\r\n\t\tpressedEnter: function() {\r\n\t\t\talert(&quot;You pressed the Enter key!&quot;);\r\n\t\t}\r\n\t}\r\n});<!--formatted--><\/code><\/pre>\n<p>Running the code, you will see that if I enter a bunch of different characters, then nothing happens. But if I press the Enter key, we see the alert.<\/p>\n<p>I just used the Enter key as an example. There are several other key modifiers built into Vue.js, such as tab, delete, esc and space, just to mention a few. And, if you need other keys than the available key modifiers, then you can use a number as the modifier instead. This number represents the character code, exactly as in vanilla JavaScript.<\/p>\n<p>If I replace the enter key modifier with 13, which is the character code for the Enter key, then we should see that the alert is still showing.<\/p>\n<pre><code class=\"html\">&lt;input type=&quot;text&quot; v-on:keyup.13=&quot;pressedEnter&quot;&gt;<!--formatted--><\/code><\/pre>\n<p>In the previous post, I mentioned that modifiers can be chained. Perhaps I want the event listener to only be invoked when I press either the Enter og Space key.<\/p>\n<p>First I will just replace 13 by <span class=\"code\">enter<\/span> as it improves readability.<\/p>\n<pre><code class=\"html\">&lt;input type=&quot;text&quot; v-on:keyup.enter=&quot;pressedEnter&quot;&gt;<!--formatted--><\/code><\/pre>\n<p>Then after the <span class=\"code\">enter<\/span> modifier, I can add another modifier by simply adding a dot followed by the name of the modifier. In this case, I will use the <span class=\"code\">space<\/span> modifier.<\/p>\n<pre><code class=\"html\">&lt;input type=&quot;text&quot; v-on:keyup.enter.space=&quot;pressedEnter&quot;&gt;<!--formatted--><\/code><\/pre>\n<p>What this does, is that the event listener is only invoked if I press either the Enter or Space key. Before taking the code for a spin, I should probably rename the event handler to something less misleading, as well as rephrase the alert message.<\/p>\n<pre><code class=\"html\">&lt;input type=&quot;text&quot; v-on:keyup.enter.space=&quot;pressedSomethingInteresting&quot;&gt;\r\n\r\npressedSomethingInteresting: function() {\r\n\talert(&quot;You pressed the Enter or Space key!&quot;);\r\n}<!--formatted--><\/code><\/pre>\n<p>Now let\u2019s test it out and verify that it works.<\/p>\n<p>That\u2019s all there is to key modifiers in Vue.js.<\/p>\n<p>\n<script async src=\"\/\/jsfiddle.net\/Andy0708\/q4jw7g3r\/1\/embed\/js,html,result\/dark\/\"><\/script>\n<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"series\">Part 12 of 55 in the <a href=\"https:\/\/codingexplained.com\/tutorial\/vue-js-beginner-to-professional\" class=\"series-165\" title=\"Vue.js: From Beginner to Professional\">Vue.js: From Beginner to Professional<\/a> series<\/div><p>In the previous post, we took a look at event modifiers. In this post, we will be looking at a more specialized category of event modifiers, namely so-called key modifiers. As the name suggests, this category of event modifiers can be used with keyboard events. Say that we have a text input, for example, and&hellip; <a href=\"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers\" class=\"more-link\">read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"Vue Key Modifiers","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[164],"tags":[168,166,167],"series":[165],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Vue.js Key Modifiers<\/title>\n<meta name=\"description\" content=\"Learn how to use key modifiers in Vue.js, both using key modifier names and character codes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Vue.js Key Modifiers\" \/>\n<meta property=\"og:description\" content=\"Learn how to use key modifiers in Vue.js, both using key modifier names and character codes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-27T00:06:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T19:46:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers\",\"url\":\"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers\",\"name\":\"Vue.js Key Modifiers\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2017-02-27T00:06:37+00:00\",\"dateModified\":\"2017-06-11T19:46:24+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Learn how to use key modifiers in Vue.js, both using key modifier names and character codes.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Vue Key Modifiers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Vue.js Key Modifiers","description":"Learn how to use key modifiers in Vue.js, both using key modifier names and character codes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers","og_locale":"en_US","og_type":"article","og_title":"Vue.js Key Modifiers","og_description":"Learn how to use key modifiers in Vue.js, both using key modifier names and character codes.","og_url":"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2017-02-27T00:06:37+00:00","article_modified_time":"2017-06-11T19:46:24+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers","url":"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers","name":"Vue.js Key Modifiers","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2017-02-27T00:06:37+00:00","dateModified":"2017-06-11T19:46:24+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Learn how to use key modifiers in Vue.js, both using key modifier names and character codes.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/front-end\/vue-js\/vue-key-modifiers#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Vue Key Modifiers"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-FG","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2584"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=2584"}],"version-history":[{"count":4,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2584\/revisions"}],"predecessor-version":[{"id":3006,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2584\/revisions\/3006"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=2584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=2584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=2584"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=2584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}