javascript - Get keycode/key from string -
i have select
tag in giving option f1
f10
..now when user press button 1 f1
f10
have detect.
<select id="forward_ckey"> <option value="default"></option> <option value="esc" disabled>esc</option> <option value="f1">f1</option> <option value="f2" disabled>f2</option> <option value="f3" selected>f3</option> <option value="f4" disabled>f4</option> <option value="f5" disabled>f5</option> <option value="f6">f6</option> <option value="f7">f7</option> <option value="f8">f8</option> <option value="f9">f9</option> <option value="f10">f10</option> <option value="f11">f11</option> <option value="f12">f12</option> </select> $(document).unbind('keydown.g').bind('keydown.g', function (e) { var unicode = e.keycode; if (unicode == 'selected value keycode') { //here "f3" keycode
output: if press f3 , selected value f3 should execute code block in that.. main problem facing got f3
option value , getting keycode keypress how match both..either need key of pressed keyboard or keycode of selected value option
demo
var selected_id = ""; $('select').on('change', function() { selected_id = this.value; }); $(document).bind('keydown', function(e) { var unicode = e.keycode; alert(""); if (unicode == selected_id) { //f3 keycode or key `e` alert(""); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="play_ckey"> <option value="default"></option> <option value="esc" selected>esc</option> <option value="f1">f1</option> <option value="f2" disabled>f2</option> <option value="f3" disabled>f3</option> <option value="f4" disabled>f4</option> <option value="f5" disabled>f5</option> <option value="f6">f6</option> <option value="f7">f7</option> <option value="f8">f8</option> <option value="f9">f9</option> <option value="f10">f10</option> <option value="f11">f11</option> <option value="f12">f12</option> </select>
i got value selected option how can key pressed user or keycode of selected value option match in condition?
to keycode of corresponding selected value need this:
var keycodes = {'f1': 112, 'f2': 113, 'f3': 114, …}; var selectedvalue = $('#forward_ckey').val(); var selectedkeycode = keycodes[selectedvalue];
Comments
Post a Comment