Fire event on Control+C, but still allow copy - javascript

I'm trying to run some code when the user presses control+c on a webpage, but I still want that key action to copy whatever the user has selected. With my current code, only the events I write actually happen and the default action is ignored.
Is this possible? Here's the code I'm using:
Code for control key functionality
$.ctrl = function(key, callback, args) {
var isCtrl = false;
$(document).keydown(function(e) {
if(!args) args=[]; // IE barks when args is null
if(e.ctrlKey) isCtrl = true;
if(e.keyCode == key.charCodeAt(0) && isCtrl) {
callback.apply(this, args);
return false;
}
}).keyup(function(e) {
if(e.ctrlKey) isCtrl = false;
});
};
Code for Control+C:
$('input').click(function(){
$(this).addClass('selected');
$(this).focus();
$(this).select();
});
$('input').blur(function(){
$('input.selected').removeClass('selected');
});
$.ctrl('C', function() {
if( $('input.selected').length != 0 )
{
alert("Saved");
}
});

Remove the line return false;

Related

Trap keypress using jquery

I have written a code block to trap key press on Ctrl + sit works and an alert has been placed when the event has ran to show me that the code block has successfully ran. later this code block will be replaced by a save command.
However I am having some trouble as the first time Ctrl + s is pressed it works however every time afterwards the s button alone triggers the event
here is the code block:
var isCtrl = false;
ck.on('contentDom', function (evt) {
ck.document.on('keyup', function (event) {
if (event.data.$.keyCode === 17) isCtrl = false;
});
ck.document.on('keydown', function (event) {
if (event.data.$.keyCode === 17) isCtrl = true;
if (event.data.$.keyCode === 83 && isCtrl === true) {
//The preventDefault() call prevents the browser's save popup to appear.
//The try statement fixes a weird IE error.
try {
event.data.$.preventDefault();
} catch (err) { }
alert('ctrl-s');
return false;
}
});
}, ck.element.$);
}
any help is greatly appreciated.
This is a way to perform ctrl+s command in jquery that works every time:
$(window).keypress(function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
alert("Ctrl-S pressed");
event.preventDefault();
return false;
});
This post is for anyone who has also had trouble with this problem, I have now solved it. The problem with my original code block is that once isCtrl was set as true it remained true. which meant that when time came again to check if it was pressed it didn't matter whether it was pressed again as it was already set to true. there for I have added a line to change it back to false when the conditional statements have ran.
Here is the new code block:
var isCtrl = false;
ck.on('contentDom', function (evt) {
ck.document.on('keyup', function (event) {
if (event.data.$.keyCode === 17) isCtrl = false;
});
ck.document.on('keydown', function (event) {
if (event.data.$.keyCode == 17) isCtrl = true;
if (event.data.$.keyCode == 83 && isCtrl === true) {
//The preventDefault() call prevents the browser's save popup to appear.
//The try statement fixes a weird IE error.
try {
event.data.$.preventDefault();
} catch (err) { }
alert('ctrl-s');
isCtrl = false;
return false;
}
});
}, ck.element.$);
}

show javascript message to user using onbeforeunload event only for browser close event

As per the project requirement, on which I am working now, I need to show user a javascript alert only in the event of browser close using javascript. All other page events like url click, button click, F5 key press etc. are to be disregarded. I have tried with the following code but with no use.
var isPostBack = false;
$(function() {
// You would copy this for select and any other form elements I forgot about
$('input').live('click', function() { isPostBack = true; });
$('a').live('click', function() { isPostBack = true; });
document.onkeydown = function(e) { //attach to key down event to detect the F5 key
isPostBack = false;
if (!e) { //Firefox and Safari gets argument directly.
e = window.event;
}
var key = e.keyCode ? e.keyCode : e.which;
try {
if (key == 116) { //F5 Key detected
isPostBack = true;
}
}
catch (ex) { }
}
});
window.onbeforeunload = check;
function check() {
if (!isPostBack) {
// Do your unload code
isPostBack = false;
var strPath = window.location.pathname;
if (strPath.indexOf('CustomerPortal') >= 0) {
alert('Customer, you are leaving our page.');
}
else {
alert('User, you are leaving our page.');
}
return "Are you sure you want to exit this page?";
}
}
Please help to achieve my target requirement with your valuable comments and help.

prevent default 'F1' event in iE11

When the user press F1 key,I am planning to display our application help and suppress default action.
I tried with different options not to show help popup of IE.
Here is my Code:
document.addEventListener('keydown', function (e) {
if (e.key === 'F1' || e.keyCode == 112) {
e.cancelBubble = true;
e.cancelable = true;
e.stopPropagation();
e.preventDefault();
e.returnValue = false;
//my help menu code goes here
}
});
Please let me know how can i achieve in showing the help page of my application instead of IE help.
I am using IE11 version.
You could subscribe to the window.onhelp event:
window.onhelp =function() {
alert();
return false;
}
Try doing this
<script>
$(document).ready(function () {
removedefaulthelp();
function removedefaulthelp()
{
window.onhelp = function () {
return false;
alert();
}
}
document.addEventListener('keydown', function (e) {
if (e.key === 'F1' || e.keyCode == 112) {
removedefaulthelp();
e.cancelBubble = true;
e.cancelable = true;
e.stopPropagation();
e.preventDefault();
e.returnValue = false;
//my help menu code goes here
}
});
}
</script>
Refer this for more information.
Here is an example similar to Sukanya's answer, but my solution shows how to extend for the F2-F12 keys, and purposely disregards F-combination keys, such a CTRL + F1.
<html>
<head>
<!-- Note: reference your own JQuery library here -->
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
</head>
<body>
<h1>F-key trap example</h1>
<div><h2>Example: Press the 'F1' key to open help</h2></div>
<script type="text/javascript">
//uncomment to prevent on startup
//removeDefaultFunction();
/** Prevents the default function such as the help pop-up **/
function removeDefaultFunction()
{
window.onhelp = function () { return false; }
}
/** use keydown event and trap only the F-key,
but not combinations with SHIFT/CTRL/ALT **/
$(window).bind('keydown', function(e) {
//This is the F1 key code, but NOT with SHIFT/CTRL/ALT
var keyCode = e.keyCode || e.which;
if((keyCode == 112 || e.key == 'F1') &&
!(event.altKey ||event.ctrlKey || event.shiftKey || event.metaKey))
{
// prevent code starts here:
removeDefaultFunction();
e.cancelable = true;
e.stopPropagation();
e.preventDefault();
e.returnValue = false;
// Open help window here instead of alert
alert('F1 Help key opened, ' + keyCode);
}
// Add other F-keys here:
else if((keyCode == 113 || e.key == 'F2') &&
!(event.altKey ||event.ctrlKey || event.shiftKey || event.metaKey))
{
// prevent code starts here:
removeDefaultFunction();
e.cancelable = true;
e.stopPropagation();
e.preventDefault();
e.returnValue = false;
// Do something else for F2
alert('F2 key opened, ' + keyCode);
}
});
</script>
</body>
</html>

document keydown bind not working for input field

I have been using jquery hotkeys plugin from jeresig's hotkey. Shortcuts work fine when the document is in focus, but when the focus is in input fields, the shortcuts are not working. I have used $(document) or $(document).find('input') for binding. But these are not working either.
I have used this following code for making shortcut:
$(document).ready(function(){
shortcutsInit();
});
function shortcutsInit(){
$(document).bind('keydown', "shift+f2", function() {
window.location.replace("/list");
return false;
});
$(document).bind('keydown', "f3", function() {
if($('#searchholder').length){
$('#searchholder').focus();
}
console.log('f3 pressed');
return false;
});
}
try it :
$(document).ready(function(){
$(document).on("keydown", function(e){
if(e.shiftKey && (e.which || e.keyCode || e.charCode) == 113){
window.location.replace("/list");
return false;
}
if((e.which || e.keyCode || e.charCode) == 114){
if($('#searchholder').length)
$('#searchholder').focus();
console.log('f3 pressed');
return false;
}
});
});
Maybe these options are solving the problem:
$.hotkeys.options.filterInputAcceptingElements = false;
$.hotkeys.options.filterTextInputs = false;

Preventing user from typing text into a text box but allowing paste keyboard shortcut

I was wondering if there was a way to disable all key-down events (such as typing text) while the text box element is selected/has focus but still allow the ctrl+v paste shortcut to be used. In other words I'd like to make it so the only way a user can enter any text into the box is by pasting it (either by using the context menu or by the ctrl+v).
Try this simple solution:
<input type="text" id="txtbox" />
$(document).ready(function () {
var ctrlKey = 17
var vKey = 86
$("#txtbox").keydown(function (e) {
if (!(e.ctrlKey && e.which == vkey)) {
return false;
}
});
});
DEMO
Here's the code, deactivate keydown, but keep Ctrl+v working.
http://jsfiddle.net/70h3zron/
var ctrlActive = false;
$('#idtext').keydown(function(e) {
if(ctrlActive)
{
if(event.which == 86) {
return true;
}
else
{
e.preventDefault();
return false;
}
}
else
{
if(event.which == 17)
ctrlActive = true;
else
{
e.preventDefault();
return false;
}
}
});
$('#idtext').keyup(function(e) {
if(event.which == 17) {
ctrlActive = false;
}
});
Use this in jQuery,
$('#yourTextElementId').keypress(function(){
return false;
)};
Demo

Categories