views:

70

answers:

3

I'm using a Wiki application that has buttons to indent and outdent list items. I'd like to write a greasemonkey script to capture Ctrl+M and Ctrl+Shift+M for indent and outdent, respectively. Is this possible?

+2  A: 

Yes it is possible, I've used scripts that rely on hotkeys before. I'm not familiar with writing greasemonkey scripts, but since it's just JavaScript, I believe you can use jQuery and a plugin like this one http://plugins.jquery.com/project/hotkeys. Using that plugin, doing what you want to do is as simple as

$(document).bind('keydown', 'Ctrl+M', fn);

Zach Bialecki
@Zach I like your thinking. Is it possible to include a separate JS library within a greasemonkey script? I always pull down JS libraries in the HTML header and wouldn't know how else to do that.
Ben McCormack
@Ben yes it is possible to use an external library, but I would not recommend it for #ux reasons (it's more data to download, load, and keep in memory, and it will only make your code slower than pure JS)
Erik Vold
@Erik, @Ben Different methods for including jQuery and plugins with Greasemonkey are discussed in numerous StackOverflow questions, but if you use `@require`, the user will only have to download the additional kilobytes once, on installing the script (not much harm to user experience, I'd say). I also haven't found jQuery code to be significantly slower than pure JavaScript, though it is many times faster to write.
npdoty
@npdoty I know all about the different methods of including jQuery.. You say you only download it once, well 1 > 0. You say it doesn't effect speed much, that's true, and obviously the user may not notice it, but it does effect speed for all of the reasons I already stated. I personally do not install userscripts that use jQuery because I don't want jQuery in memory 20x per page for obvious reasons.. you say it's faster to write, well that's an extremely minor point imo, it's better to know javascript than jquery because you won't always have jquery, and you shouldn't for a userscript imho..
Erik Vold
by all means use jQuery if you want to get something working fast, but think about removing it asap please.
Erik Vold
in this case though the difference in the amount of code between using jQuery vs not using it, is like 1 or 2 lines of code, it's not worth loading a library for that imho.
Erik Vold
@Erik In this particular instance, the "user" is just me. I'm just trying to hack my personal use of a web page so that I can use a hot key instead of having to click a button. Also, I'm not sure what you mean by "you won't always have jquery." I'll just link to Microsoft's CDN hosting of the file, so I should (almost) always have it if I'm connected to the internet.
Ben McCormack
@Ben well, like I said it's fine to use jQuery. What I meant by "you won't always have jquery." is that it's probably worth learning how to do this without jquery, because it's not hard, and there may be another time when you need to do this, and using jquery is not an option, I don't know you so maybe that'll never happen idk.
Erik Vold
A: 

You can probably do this pretty easily. You'd have to add event listeners to the input (textarea?). Here's an event tester so you can figure out what keyboard event properties and values you are hunting for: http://unixpapa.com/js/testkey.html

For example, control-shift-M gives you this keyup event in Firefox:

keyup     keyCode=77 (M)    which=77 (M)    charCode=0        
          keyIdentifier=undefined keyLocation=undefined
          shiftKey=true ctrlKey=true altKey=false metaKey=false
Robusto
A: 

If you want some documentation on how to use key events, then read the MDC documentation here. Basically though you just want to add an event listener to either the document or some element within the document, then the event listener should determine if the desired keys have been pressed (and only those keys), if so then do the corresponding action.

Erik Vold