Guest User

Untitled

a guest
Nov 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. global.display.add_keybinding('random-name',
  2. new Gio.Settings({schema: 'org.gnome.shell.keybindings'}),
  3. Meta.KeyBindingFlags.NONE,
  4. function() { /* ... some code */ });
  5.  
  6. KeyManager: new Lang.Class({
  7. Name: 'MyKeyManager',
  8.  
  9. _init: function() {
  10. this.grabbers = new Map()
  11.  
  12. global.display.connect(
  13. 'accelerator-activated',
  14. Lang.bind(this, function(display, action, deviceId, timestamp){
  15. log('Accelerator Activated: [display={}, action={}, deviceId={}, timestamp={}]',
  16. display, action, deviceId, timestamp)
  17. this._onAccelerator(action)
  18. }))
  19. },
  20.  
  21. listenFor: function(accelerator, callback){
  22. log('Trying to listen for hot key [accelerator={}]', accelerator)
  23. let action = global.display.grab_accelerator(accelerator)
  24.  
  25. if(action == Meta.KeyBindingAction.NONE) {
  26. log('Unable to grab accelerator [binding={}]', accelerator)
  27. } else {
  28. log('Grabbed accelerator [action={}]', action)
  29. let name = Meta.external_binding_name_for_action(action)
  30. log('Received binding name for action [name={}, action={}]',
  31. name, action)
  32.  
  33. log('Requesting WM to allow binding [name={}]', name)
  34. Main.wm.allowKeybinding(name, Shell.ActionMode.ALL)
  35.  
  36. this.grabbers.set(action, {
  37. name: name,
  38. accelerator: accelerator,
  39. callback: callback,
  40. action: action
  41. })
  42. }
  43.  
  44. },
  45.  
  46. _onAccelerator: function(action) {
  47. let grabber = this.grabbers.get(action)
  48.  
  49. if(grabber) {
  50. this.grabbers.get(action).callback()
  51. } else {
  52. log('No listeners [action={}]', action)
  53. }
  54. }
  55. })
  56.  
  57. let keyManager = new KeyManager()
  58. keyManager.listenFor("<ctrl><shift>a", function(){
  59. log("Hot keys are working!!!")
  60. })
  61.  
  62. const Lang = imports.lang
  63. const Meta = imports.gi.Meta
  64. const Shell = imports.gi.Shell
  65. const Main = imports.ui.main
  66.  
  67. for (let it of keyManager.grabbers) {
  68. global.display.ungrab_accelerator(it[1].action)
  69. Main.wm.allowKeybinding(it[1].name, Shell.ActionMode.NONE)
  70. }
Add Comment
Please, Sign In to add comment