Page 1 of 2

my_custom_keys plugin stub

Posted: 19 Mar 2013, 00:48
by cjbnc
This is a stub. Rather than try to have everyone's favorite hotkey settings added to the code as an infinite list of new plugins, it seemed like a good idea to provide one stub plugin that anyone could easily customize to fit their own site. I simply modified the swap_jk plugin and added a lot of comments and a couple examples. Note that it already includes the new *=Shift prefix.

plugins/my_custom_keys/init.php-dist:

Code: Select all

<?php
// Plugin: my_custom_keys
//    provides a hotkey plugin that can be customized to your site
//
// By default, this plugin does nothing.
// You must enable it, and add your own hotkey maps.
// (Why? So future updates will not overwrite your customized init.php.
//  They will only update the init.php-dist file.)
//
// To enable the script:
//     cd plugins/my_custom_keys (this directory)
//     cp init.php-dist init.php
//
// Next, edit your copy init.php and customize the hook_hotkey_map function.
//
// Finally, reload your tt-rss web session. Enable the plugin in
//   Actions -> Preferences -> Plugins -> User Plugins.
//

// What if you want per-user hotkeys?
//     There's currently no way for a user to customize their own keys.
//     You can provide multiple hotkey plugins, if you like.
//     To do so:
//         make a new plugins/user1_keys directory
//         copy this init.php file to that directory
//         change this class name to a unique name, e.g. "User1_Custom_Keys"
class My_Custom_Keys extends Plugin {

   private $link;
   private $host;

// You can change the description of this plugin here
   function about() {
      return array(1.0,
         "Enable site-specific hotkey maps",
         "cjbnc");
   }

   function init($host) {
      $this->link = $host->get_link();
      $this->host = $host;

      $host->add_hook($host::HOOK_HOTKEY_MAP, $this);
   }

// This is where the hotkey maps are defined. You can uncomment
// the example codes below, or add your own.
//
// Each map looks like this:
//      $hotkeys[KEYS] = "KEY_FUNCTION";
//
// KEYS can be:
//      "n"             = a single key character
//      "*n"            = Shift + key character  (Shift-n)
//      "^n"            = Ctrl + key character   (Ctrl-n)
//      "f q"           = a sequence of two keys
//      "(37)|left"     = a javascript key code and label
//      "^(38)|Ctrl-Up" = can use * Shift or ^ Ctrl with key codes
//  (search the web for "javascript key codes" for more examples)
//
// KEY_FUNCTION can be any of the functions defined by
// get_hotkeys_info() located in the file include/functions.php
//
// The default hotkey bindings are defined by
// get_hotkeys_map()  also located in the file include/functions.php

   function hook_hotkey_map($hotkeys) {

        // Example: Swap the functions of the j/k keys for vim users

      // $hotkeys["j"] = "next_feed";
      // $hotkeys["k"] = "prev_feed";

        // Example: Arrow key navigation

        // $hotkeys["(37)|left"]  = "prev_article";
        // $hotkeys["(39)|right"] = "next_article";
        // $hotkeys["(38)|up"]    = "article_scroll_up";
        // $hotkeys["(40)|down"]  = "article_scroll_down";

      return $hotkeys;

   }
}

// vim:ft=php
?>


This is on github so I can make it a Pull Request if you like, fox.

Re: my_custom_keys plugin stub

Posted: 19 Mar 2013, 01:08
by fox
It's probably better off stickied here and in the KB maybe; otherwise people would try to enable it and whine that nothing works or whatever.

Re: my_custom_keys plugin stub

Posted: 19 Mar 2013, 19:08
by cjbnc
Another example, to provide similar arrow-key navigation, per this post: viewtopic.php?t=1399&p=6163

Code: Select all

      $hotkeys['(13)|enter']   = 'open_in_new_window';
      $hotkeys['(37)|left']    = 'prev_article_noscroll';
      $hotkeys['(38)|up']      = 'article_scroll_up';
      $hotkeys['(39)|right']   = 'next_article_noscroll';
      $hotkeys['(40)|down']    = 'article_scroll_down';
      $hotkeys['*(38)|s-up']   = 'prev_feed';
      $hotkeys['*(40)|s-down'] = 'next_feed';

Re: my_custom_keys plugin stub

Posted: 18 Apr 2013, 19:40
by cjbnc
Updated to remove the get_link() call per the recent trunk changes.

Code: Select all

<?php
// Plugin: my_custom_keys
//    provides a hotkey plugin that can be customized to your site
//
// By default, this plugin does nothing.
// You must enable it, and add your own hotkey maps.
// (Why? So future updates will not overwrite your customized init.php.
//  They will only update the init.php-dist file.)
//
// To enable the script:
//     cd plugins/my_custom_keys (this directory)
//     cp init.php-dist init.php
//
// Next, edit your copy init.php and customize the hook_hotkey_map function.
//
// Finally, reload your tt-rss web session. Enable the plugin in
//   Actions -> Preferences -> Plugins -> User Plugins.
//

// What if you want per-user hotkeys?
//     There's currently no way for a user to customize their own keys.
//     You can provide multiple hotkey plugins, if you like.
//     To do so:
//         make a new plugins/user1_keys directory
//         copy this init.php file to that directory
//         change this class name to a unique name, e.g. "User1_Custom_Keys"
class My_Custom_Keys extends Plugin {

   private $host;

// You can change the description of this plugin here
   function about() {
      return array(1.0,
         "Enable site-specific hotkey maps",
         "cjbnc");
   }

   function init($host) {
      $this->host = $host;

      $host->add_hook($host::HOOK_HOTKEY_MAP, $this);
   }

// This is where the hotkey maps are defined. You can uncomment
// the example codes below, or add your own.
//
// Each map looks like this:
//      $hotkeys[KEYS] = "KEY_FUNCTION";
//
// KEYS can be:
//      "n"             = a single key character
//      "*n"            = Shift + key character  (Shift-n)
//      "^n"            = Ctrl + key character   (Ctrl-n)
//      "f q"           = a sequence of two keys
//      "(37)|left"     = a javascript key code and label
//      "^(38)|Ctrl-Up" = can use * Shift or ^ Ctrl with key codes
//  (search the web for "javascript key codes" for more examples)
//
// KEY_FUNCTION can be any of the functions defined by
// get_hotkeys_info() located in the file include/functions.php
//
// The default hotkey bindings are defined by
// get_hotkeys_map()  also located in the file include/functions.php

   function hook_hotkey_map($hotkeys) {

        // Example: Swap the functions of the j/k keys for vim users

      // $hotkeys["j"] = "next_feed";
      // $hotkeys["k"] = "prev_feed";

        // Example: Arrow key navigation

        // $hotkeys["(37)|left"]  = "prev_article";
        // $hotkeys["(39)|right"] = "next_article";
        // $hotkeys["(38)|up"]    = "article_scroll_up";
        // $hotkeys["(40)|down"]  = "article_scroll_down";

      return $hotkeys;

   }
}

// vim:ft=php
?>

Re: my_custom_keys plugin stub

Posted: 20 Apr 2013, 00:48
by jmozmoz
You have to add

Code: Select all

        function api_version() {
                return 2;
        }


Re: my_custom_keys plugin stub

Posted: 25 Apr 2013, 12:25
by ArchCarrier
What's the safest way to disable all existing hotkeys? I don't want to touch core files, so I tried adding

Code: Select all

unset($GLOBALS['hotkeys']);

to the init function, but that doesn't work. Any ideas?

Re: my_custom_keys plugin stub

Posted: 25 Apr 2013, 13:07
by phz
ArchCarrier wrote:What's the safest way to disable all existing hotkeys? I don't want to touch core files, so I tried adding

Code: Select all

unset($GLOBALS['hotkeys']);

to the init function, but that doesn't work. Any ideas?

Return an empty array in the `hook_hotkey_map()` function.

If you meant that you want to disable all built-in hotkeys and just use the ones you define yourself, just return an array with your own definitions in the above mentioned function (i.e. create a new array instead of reusing the function argument and return that).

Re: my_custom_keys plugin stub

Posted: 25 Apr 2013, 16:28
by ArchCarrier
Thanks, that worked!

Re: my_custom_keys plugin stub

Posted: 26 Apr 2013, 22:03
by GravityWell
Thanks for this plugin. I used the following:

Code: Select all

$hotkeys['(39)|right']   = 'next_article_noscroll';
$hotkeys['(37)|left']    = 'prev_article_noscroll';

Nice to have that option for feeds with large articles.

Re: my_custom_keys plugin stub

Posted: 24 May 2013, 17:38
by GravityWell
Any idea how to get page down functionality mapped to a key? In Chrome, if the focus is in the article list, page down (or space bar) advances to the next visible page of articles. The issue is getting the focus to the article list is a bit awkward, especially on a tablet, and I'd rather set pgup/pgdn to perform this function globally, even if the focus is in the feed list.

Re: my_custom_keys plugin stub

Posted: 24 May 2013, 21:10
by ArchCarrier
Yes, I've noticed this as well. It appears to be a bug.

Re: my_custom_keys plugin stub

Posted: 30 Jul 2013, 13:59
by recognitium
Hi everybody,

--edit --
first part of question solved. Sorry for not being good at forum searches.
--end edit--

As a side issue, for those who are in fact developers / have experience with tt-rss code and plugins : do you think it would be achievable/easy to try to make articles open/select in the position they are? As for now, whenever an article is select for opening, it becomes the first on the list. (i wonder if maybe this could be so easy as to create a custom theme mod)

Thanks for the patience and for all the great work around tt-rss

Re: my_custom_keys plugin stub

Posted: 16 Feb 2014, 21:22
by timmmmyboy
Is there any way to use this plugin to hook into the share buttons and article note feature at the bottom of each article? Anyone doing anything like that? I use keyboard shortcuts for most of what I do in TT-RSS and it would be convenient to not have to scroll and find buttons to click on.

Re: my_custom_keys plugin stub

Posted: 16 Feb 2014, 23:16
by fox
You can put arbitrary javascript in a hotkey handler so yes you can do that.

Re: my_custom_keys plugin stub

Posted: 17 Feb 2014, 03:12
by timmmmyboy
Thanks! I'll give it a shot.