here is my issue:
I want to know which key got typed whenever I type something on a page and
window.addEventListener("keydown", function(event) {
console.log(event.key);
});
works fine but I recently found out issues with it.
I will give an example related to the backtick character (`), but keep in mind that it doesn't affect only this character.
I have an AZERTY keyboard, here is my layout :
Whenever I press "Right-Alt + Key7", it should log the backtick character but logs "รจ". I found out that the on-screen azerty keyboard of windows has a different key, this one: there's no backtick So I pretty much understand why it doesn't log backtick, my azerty layout isn't the windows' one or stuff like that. But then my question is: Why does it prints backtick whenever I type on a textarea for example? It is working everywhere excepts on keyevents. And most importantly how? How can I, too, get the right keys everytime?
I began looking for tricks to get the rights keys like putting a non-visible contenteditable div in front of the page to take its text... it could work, but you might understand why I don't want to go that path. Is there any other tricks or solutions that are prettier?
I hope I made myself understood, thanks you.
The issue was that I was using the "keydown" event and not the "keypress" one which works as intended, sorry for bothering and I hope people with the same issue will be able to find this!
Related
[NOTE]: I've checked SO answers which might have worked 5 years ago which don't seem to work. Please don't copy-paste that junk. thank you.
task:
Take a string, conver it into sequece of keystrokes, do something with that sequence and ultimately come to the conclusion that the webpage you want to put that string in has gone through everything it needs to go through as if the string characters were sequentially pressed by a human through a keyboard.
Question:
How the hell is this done?
element.value = 'test123' // BAD EXAMPLE
if you don't know why this is a bad example, don't answer this question.
element.dispatchEvent(/*event for keydown*/)
element.dispatchEvent(/*event for textInput*/)
element.dispatchEvent(/*event for keyup*/)
^ this... this should technically work, however I've not found a working example.
Additionally apparently there may be some issues with event.isTrusted which I don't know how to get around.
... maybe I'm not using the right tools...
chrome.input.ime ... this seems to be related to chromiumOS and chrome doesn't react to it. (at least to what i wrote (i'm using windows) )
maybe chrome.debugger.... ??? though I can't figure out how to dispatch a keyboard event to an element through this api
TL/DR
Please post a valid example (which works beyond element.onkeydown(***));
which results in a keystroke being registered by a webpage as if it originated from a keyboard (or OS) when in fact it's from javascript.
From here:
jQuery has a .keypress method accepting no arguments that simulates a keypress.
$("#target").keypress();
Will trigger a keypress on #target
If you'd like to also select which key was pressed, you can use .trigger. This example is from the docs:
var e = $.Event("keydown", { keyCode: 8}); //"keydown" if that's what you're doing
$("body").trigger(e);
The key code 8 is the key code for backspace in JavaScript.
Let me know how that works for you :)
When I am editing a JavaScript file inside VSCode and try using the backticks (`) it won't auto close. I want to have the same auto-closing feature that I have with the quotation marks, where if I type " I get:
"|"
(| = my cursor)
I have tried disabling all my extensions and that didn't solve it. If I try it inside Atom it works just fine.I'm using a Belgian keyboard layout on Mac.
A now-deleted answer referred to this fixed bug, but unfortunately for me, typing a space after the backtick (as described in the fix) just leaves me with a backtick and a space.
I haven't tried to find a way around this, but at least on my system (set to German) the behavior described seems to come from the fact that by default the backtick (`) is a dead key, meaning it will not produce a character until the next key is hit. Funnily enough, the backtick that you see on screen is thus actually created because VSCode tries to auto-complete the backtick (with another backtick), but the initial character is swallowed (because there is no such thing as an accented accent, which is what that key is generally used for).
If I type a space after the backtick, it gets auto-completed though. Running VSCode 1.36.0 on MacOS.
I hope my explanation is clear enough.
Setting "editor.autoClosingQuotes": "always" in VSCode settings fixed this for me.
EDIT: After using this for a couple of weeks it got really annoying always deleting the second quote when it added it even in inappropriate places, like at the beginning or middle of words. This is still the best solution I know of, but it's not perfect.
After weeks of trying and testing to find a solution for my needs I admit that I still have no idea how I can solve this problem.
It sounds simple: I want that a user is able to mention things in a text area similar to twitter.
The problem is that I can't seem to manage it to make it work. Every browser has its own specialties which are coming into my way and break things. I have tried multiple different attempts but none of them worked even on a single browser completely.. mixing text and HTML appears to be incredibly hard to do.
So here I am. Asking you guys for any kind of help. Whether it's a library you can recommend me that is already doing what I need here, or if you did something similar and can tell me what exactly you did to make this work on multiply browsers.
My current solution looks something like this: Hitting # will insert a input text field into a div contenteditable everything is working nice so far unless the whole thing is the first element of a row. If the caret is also at position 0 and the user hits Enter, then something dies inside the browser which removes the whole input box without further notice or any events - at least not on Chrome. That was the most promising solution that I was able to come up with. Don't think I didn't try to save it by inserting e.g. a native Text with a zero-whitespace-character but that doesn't work either. It works better - but not completely.
I'm really frustrated by now and this is holding my whole project back which has this key feature that has to work properly - mainly because the information put there is going to be persisted as XML but that is a completely different story.
I really hope somebody can help me to get a solution for this. Bear in mind that I am actually using GWT 2.8.0 but I would not mind to use/wrap a JavaScript library at this point ..
I need to implement a feature that copy-pastes some custom data across our websites using javascript.
After a lot of stumbling blocks, this question's lead answer pointed me in the right direction.
Basically, to copy, I add a new textarea to my document, set its value to my desired text, and run document.execCommand('copy').
So far so good.
I attempting to implement paste similarly, and there lies the problem.
I add a textarea, focus and select it, and run document.execCommand('paste'); but it doesn't work.
Here's a fiddle demonstration:
https://jsfiddle.net/tusharx23/7zpjfh2h/1/
As you can see, the log says that 'paste was unsuccessful'
Can anyone help me get the paste to work?
Thank you.
Moving from InDesign to Acrobat now, I need to automate a very simple task. I'll eventually use BridgeTalk to have an InDesign script call Acrobat X and have it do a couple of simple things. To this end I've been reading up on how to script Acrobat. Unfortunately, it does not seem anywhere as simple as scripting InDesign or Illustrator.
For one thing, the ExtendScript Toolkit is now useless since Acrobat has a built-in "Javascript Console". This would be perfectly fine, except that my console seems to be completely broken. Once I launched it (and it was hellacious just trying to figure that out), I type in a simple 3 + 4 in the console and press Command+Enter, as noted in Thom Parker's guide on AcrobatUsers. (I don't have a fancy Mac extended keyboard with a numeric keypad, so I have to use Command+Enter.)
Nothing happens.
I've tried other things, such as selecting the code first, pressing Control+Enter instead, using a different line of code such as console.println("Hello.");, etc. Nothing I do seems to work. What am I doing wrong?
Finally got my question answered over on Adobe's forums. I thought I had tried everything, but it turns out that I had not even tried a simple Shift + Enter. Hard to believe, since I thought I'd tried every possible key combination already, but I cannot deny the facts. Since I only had the small Apple keyboard without the numeric keypad, I suppose this is the only way to get Enter instead of Return from that key. At any rate, the answer is now known!
It seems to be command-return on MacOS 10.13.6 But thanks for the hint above.