Do you know how to press Enter key with Zombie.js ?
Thanks in advance.
There's no way to do that yet using just zombie API. It's because .fire() method doesn't allow you to pass any event data in addition to event name (which would be necessary to state which keyCode is associated with that key event).
WTK is correct and there's no native way in zombie.js but I think you could add a javascript function to simulate the enter key press and trigger it from zombie.js like so:
If you have access to the page source, add a function on your page to simulate the enter key press:
function pressEnterKey(elmSelector){
elmSelector = elmSelector || 'document'
var e = jQuery.Event("keypress");
e.which = 13;
e.keyCode = 13;
$(elmSelector).trigger(e);
}
Trigger it from zombie.js:
browser.evaluate("pressEnterKey()");
If you don't have access to the source you could inject the script on the page using something like this. Remember to use browser.wait after to ensure the page is ready:
var injectedScript = browser.document.createElement("script");
injectedScript.setAttribute("type","text/javascript");
injectedScript.innerText = '...pressEnterKey function text here...'
browser.body.appendChild(injectedScript);
Related
What I have
I have a simple view in my .cshtml file which uses the form
//actionName //controller //method //htmlAttributes
#using (Html.BeginForm("TableName", "Data", FormMethod.Post, new { id = "frmName" }))
{
//combobox with id
//button with id
}
I was able to implement triggering the button with Enter key by using answers from this thread as a basis.
$('#frmName').on('keypress', function (e) {
sender = event || window.event;
var hasfocus = $('#btnGo').is(':focus') || false;
if (sender.keyCode == 13 && !hasfocus) {
e.preventDefault();
$('#btnGo').trigger('click');
}
});
In above scenario, everything works as intended.
What I need
I recently found that this particular view will not be using the form. As a result, I wanted to adapt my javascript code to separate it from the frmName. However, it proved to be trickier than I anticipated.
What I tried
I tried replacing the formID with the button ID
$('#btnGo').on('keypress', function (e) {
sender = event || window.event;
var hasfocus = $('#btnGo').is(':focus') || false;
if (sender.keyCode == 13 && !hasfocus) {
e.preventDefault();
$('#btnGo').trigger('click');
}
});
I tried implementing something similar to accepted answer of the thread mentioned above.
document.getElementById("cbID")
.addEventListener("keyup", function (event) {
event.preventDefault();
if (event.keyCode == 13 || $('#btnGo').is(':focus')) {
document.getElementById("btnGo").click();
}
});
In both cases, when I select a value from the combobox and press enter key, an unexpected error pops up
Can you please point me in the right direction how I can attach Enter to button while separating it from my FormID? (Preferably, without altering my initial javascript code too much, but I'm flexible to suggestions)
Edit
After testing in different browsers(IE, Chrome, FF) for debugging purposes, I got the following results:
Initial scenario works as intended in all 3 browsers
My attempts inside Chrome leads to an error inside the browser instead of intended error checker
My attempts inside FF leads so asking for my credentials, which I am hesitant to give
Honestly, I am more interested now in why pressing Enter leads me to some kind of file saving and options provided in my picture. The question itself is still relevant though.
Short story:
// get the editor instance
var editor = CKEDITOR.instances.editor1;
// this is what I want, but it does not exist
editor.execCommand('delete');
// I've played with this, found somewhere, but without success.
editor.fire('key', { keyCode : 46 } )
Long story:
there is a problem when using the CKEditor within the Webbrowser control from .NET WindowsForms. Several keys, including the DELete key are not propagated to the control at all.
I managed to intercept the key using a global keyboard hook and sent window messages direct to the embedded IE window handle, but without success.
Now my goal is to simulate the delete key from within the javascript, because I can call a js function from my .NET app.
Somehow this must work, because it works within the virtual keyboard plugin.
(see sample)
Sadly I wasn't able to get how this works from the plugin code.
I would be glad if anybody can post a working sample.
Thanks!
I found a library used in javascript virtual keyboard (Jsvk plugin).
It is called DocumentSelection and can be found here.
<script src="./documentselection.js"></script>
function simulateDelete()
{
var editor = CKEDITOR.instances.editor1;
var container = (editor.container.getElementsByTag('textarea').getItem(0) ||
editor.container.getElementsByTag('iframe').getItem(0)
).$;
DocumentSelection.deleteAtCursor(container, true);
}
Maybe someone has an easier solution without the need of an external libaray.
I think u want some clues...
There it is...Check the Documentation
Key Event in CKEditor
alert( event.getKey() );
to get the key Element and also the other one is
alert( event.getKeystroke() == 65 );// "a" key
alert( event.getKeystroke() == CKEDITOR.CTRL + 65 );// CTRL + "a" key
alert( event.getKeystroke() == CKEDITOR.CTRL + CKEDITOR.SHIFT + 65 );//CTRL + SHIFT + "a" key
I need a JavaScript script that would disable users from closing using ALT+F4. I have looked everywhere but it just everyone just says it isn't advised.
May not even be possible, if it isn't I will just have to detect when the user does quit out this way and log it in a database.
Here is my current script, it detects if the user presses either ALT or F4, but I can't get it to cancel that key press. Is there a way to make the browser think the user pressed another key as well so the combo would be ALT + G + F4 for example, which would disrupt the ALT+F4 combo?
//Run on keydown, disable user from quiting via ALT+F4
document.onkeydown = function(evt) {
evt = evt || window.event;
//Get key unicode
var unicode = evt.keyCode ? evt.keyCode : evt.charCode;
//Check it it's ALT or F4 (115)
if (unicode == 115 || evt.altKey == 1)
{
window.event.cancelBubble = true;
window.event.returnValue = false;
}
};
That key event is (on most OSs I guess) processed by the OS before it'S even sent to the browser, so cancelling the event inside the browser won't help a thing - even if it was Javascript that is executed inside the browser's UI, not only the current document.
Therefore - what you're trying to do cannot be done.
One can prevent the resulting window closure with:
window.onbeforeunload = funcRef
I think readers landing here might want to be reminded of this (as I did).
For related details, see
Jquery prevent window closing
try to use the code below:
window.webContents.on("before-input-event",(event,input)=>{
if(input.code=='F4'&&input.alt)event.preventDefault();});
We have a dll of custom web controls based on the System.Web.UI ones. I've been asked to make it so when the user hits enter, the tab event would be fired instead. I've done this:
Me.Attributes.Remove("onkeydown")
Me.Attributes.Add("onkeydown", "if(event.keyCode == 13) {event.keyCode = 9; return event.keyCode;}")
If Me.TextMode = Web.UI.WebControls.TextBoxMode.MultiLine Then
Me.Attributes.Remove("onkeydown")
End If
But it only works on IE. When i tested it on FF and Chrome, the enter event generates postback. I've tried other solutions:
<script type="text/javascript">
function catchEnter(e){
var theKey=0;
e=(window.event)?event:e;
theKey=(e.keyCode)?e.keyCode:e.charCode;
if(theKey=="13"){ // 13 is the key code for ENTER
// here is where your code will go
//return false; // return false if you want to...
//...halt submission of form (call this function onsubmit)
}
}
</script>
function checkKey(e) {
var event = window.event ? window.event : e;
if (true) {
alert(event.keyCode)
}
}
And also:
function displayunicode(e){
var unicode=e.keyCode? e.keyCode : e.charCode
But i have to use Attributes.Add to insert the javascript. I can write down a whole javascript function using Attributes.Add? When i try to use these functions, i ignore the 'script' tags and only put the javascript code. Am i doing something wrong here?
Which one of these codes would work? (IE, FF and Chrome)
Is there a way to capture a key press event with dojo/aspect and aspect.after?
I am using a third party Javascript API (ESRI JS API v3.4) that provides a widget for drawing graphics on a map. The draw toolbar widget has an onDrawEnd event that provides the shape of the drawn graphic object as a parameter. I need to determine if the user was pressing the CTRL or SHIFT key while drawing on the map with this widget, but I use aspect.after(drawingToolbar, "onDrawEnd", myhandlerfunction, true) to connect the drawing event.
The only way I know how to determine if a key is pressed is by using an event object, which is not provided when using aspect like it is with dojo/on.
Any ideas how I can determine if a key is pressed here?
Maybe u musst go another way to catch up a Key-Event.
This is how i catch the "Enter"-Event in a Textbox. When the Enter-Key is hit, the function zoomToAnlage() is called. It's important that this event listener is already loaded in the ini-phase.
Sure, this is not a total resolve for your Question, but maybe it shows a way how you can handle it.
function initKielAnlagenNummernSuchen(){
queryTaskAnlagenNummern = new esri.tasks.QueryTask(restServicesLocation + NameSearchService + "/MapServer/23");
queryallAnlagenNummern = new esri.tasks.Query();
queryallAnlagenNummern.returnGeometry = true;
queryallAnlagenNummern.outFields = ["ANLAGE"];
require(["dojo/keys","dojo/dom","dojo/on"], function(keys, dom, on){
on(dom.byId("selectAnlagenNummer"), "keypress", function(evt){
var charOrCode = evt.charCode || evt.keyCode;
if (charOrCode == keys.ENTER) {
zoomToAnlage();
}
});
});
}
Here's a Link to dojo/keys : http://dojotoolkit.org/reference-guide/1.8/dojo/keys.html?highlight=keys#id2
Regards, Miriam
dojo.aspect can't connect to events, only functions. However you should be able to aspect to a function that is handling that event, and steal its args.
aspect.after(drawingToolbar, "someHandlerFunctionWithTheEventArg",
function(args){
// access the event from the args here
}, true);