Tienus McVinger
Tienus McVinger

Reputation: 477

Block source view from opening when ctrl+u is being pressed

I am trying to find a way to block the source view from opening when someone presses ctrl+u using javascript/jquery. The reason for this is that I'm making some sort of simple text-editor, and I made it so that ctrl+u inserts tags for underline. I got that working, except it also opens the sourcecode view, which I don't want.

Note:

I'm not trying to block users from viewing my source code all together. I've seen questions such as this one being shot down for reasons such as "people will be able to view your code anyway and there's nothing that can be done about it, it's pointless". That is not what I'm trying to accomplish here.

Upvotes: 0

Views: 140

Answers (1)

Alexis King
Alexis King

Reputation: 43872

To override a browser's native keyboard shortcut, use the Event.preventDefault() method, which will tell the browser not to handle the keyboard event as it usually would.

$(document).keydown(function (e) {
    if (e.keyCode === 85 && e.ctrlKey) {
        e.preventDefault();
        // ... your handling here ...
    }
});

Upvotes: 1

Related Questions