0

I know there are similar questions, but I could not find the answer, and I am trying to create an uncommon shortcut in order to execute action according to a specific combination of keys. I don't have an example, since I have not implemented it, I am trying to figure out if I can do it or not. I have a combination of Ctrl, shift, alt, meta keys, I can capture in javascript, my problem, is that I want to use a common key like "g" and make it behave like Ctrl, shift, alt, meta, etc, in order to combine with a key F1, F2, F3 to execute a javascript function with an specific visual component on screen. An Example, I intend to create a shortcut "Ctrl+1+F1" to perform an action in a specific table in a web page showing with more than one table on the html. Can Anyone answer me if this can be done? if someone can, How can I do it?

8
  • 2
    This question is very generic. I'm going to answer yes, you can capture the g key like you can capture the ctrl key. You should try to implement something and check where it blocks. From there, we can help you with that specific problem. Commented Apr 8, 2020 at 20:36
  • I guess you can keep track of which keys were pressed up and down, then decide if the combination is pressed from thereon. But why not just use Ctrl+F1? There seem to be enough keys on the keyboard to do pretty much anything you ever want. In fact, I'd estimate around 200-400 combinations on average keyboard using the four modifier keys. Commented Apr 8, 2020 at 20:39
  • I don't think it's possible out of the box, but you should be able to emulate it by listening to keydown and keyup events and keeping track of what is pressed and what is not. Commented Apr 8, 2020 at 20:45
  • @AntoniSilvestrovič, Because, i have a combination of Ctrl+F1 for a specific action on the component, and since i may have more than one component of the same type on the same page, i need a third key to identify the specific component i will execute the action, and i do not intend to use ctrl, shift, alt, because it will imply in limitation of number of components i can use, and also confuse the users. My doubt is because i intend to apply ergonomy, and in order to do this, i need to create a standard i can use, thats why i want to use this unusual combination of keys. Commented Apr 8, 2020 at 21:12
  • @Vilx-, i understand i can emulate this behavior with these two events, question is, how do i emulate this behavior? Commented Apr 8, 2020 at 21:17

3 Answers 3

2

A broad overview of the algorithm would be as follows:

Keep a Set-type variable that will contain all currently pressed keys. Whenever you get a keydown/keyup event, update that set by adding/removing whatever key was pressed/released. Attach the event handlers as high as you can - for example, on the <body> tag, so that you can catch everything that happens in your page.

Then at the end of the keydown event you should check if the variable contains EXACTLY the keys that your keyboard shortcut requires. If they do, launch the action. In practice you'll probably have a long list of possible keyboard shortcuts. Check them all. Maybe you'll also need extra checks, like some shortcuts could only be active when a certain control is focused or when something is in the right state or whatever.

As a variation, you might want to launch the action not when the keys are pressed, but when they are released. In that case, don't launch the action immediately, but make some sort of note in another variable, that such-and-such combination was pressed. Then wait until all the keys are released (the set is empty), and THEN launch the action. If some other keys got pressed inbetween, clear that note and check if perhaps another combination should be activated instead. Store that in the note-variable and keep waiting.

Sign up to request clarification or add additional context in comments.

Comments

0

Unfortunately, the Control keys and the Function keys seem to be mutually exclusive. You can set up key combos with any number of keys, as in the following example, but combining Control and Function doesn't work (on my system anyway).

const result = document.querySelector('#result')
const keys = {}

window.addEventListener('keydown', (ev) => {
  keys[ev.key] = true
})

window.addEventListener('keyup', (ev) => {
  if (keys['1'] && keys['Control'] && keys['F1']) {
    console.log('This does not work on my system')
  } else if (keys['1'] && keys['F1']) {
    result.value = "F1 + 1 pressed!"
  } else if (keys['Control'] && keys['o'] && keys['p']) {
    result.value = "Control + o + p pressed!"
  } else {
    setTimeout(() => result.value = "", 500)
  }
  keys[ev.key] = false
})
<p>Click in the result pane and then press F1+1, or Control+o+p. Control+F1+1 does not work.</p>
<input id="result">

3 Comments

andyvanee, when i try to press f1+1, like you asked in your example, my browser, opens help screen and does not allow me to test your example, even ctrl+o+p did not work, but, Kosh, gave me in his previous comment an answer, so far, i could set the focus, i just need to test if it is still focused on the event i need, i am still testing if it works.
Thats why i opened this forum, the variable keys you used, can not store more than one key at a time, and keydown, keyup only provide ways to deal with pre compiled modifiers, in order to solve the problem of my question the keydown, keyup, have to be extended. Thanks for the reply and time spent, if what kosh anwered, i am able to solve my problem, i will post an answer trying to explain how i solved the problem with the answers given.
Yes, there will always be problems with conflicting key combinations since your operating system and browser capture the keys first and your app comes last. The tabindex solution does not really answer the question as posed, but if it solves your problem, that's great :)
0

Thanks to everyone, compiling the answers given, i was able to solve the problem. i had to give the focus to the table (courtesy of kosh, who gave the straight answer i needed), and on the post(event) of the web page, i was capable to catch the active component. I can not post the solution i built, because the code belongs to the company, but i will try to build an example, and post on monday, in case someone else reaches this post after the same doubt (easter in brazil, and tomorrow i am travelling home, thats why the example i will show only on monday). Thanks to everyone again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.