example.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Video.js Hotkeys</title>
  6. <link href="https://vjs.zencdn.net/7.20.2/video-js.css" rel="stylesheet">
  7. <script src="https://vjs.zencdn.net/7.20.2/video.min.js"></script>
  8. <style>
  9. .video-js .vjs-menu-button-inline {
  10. width: 12em;
  11. }
  12. .vjs-menu-button-inline .vjs-menu {
  13. display: block;
  14. opacity: 1;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div>
  20. You can see the Video.js Hotkeys plugin in use below.
  21. Look at the source to see how to use it with your own videos.
  22. </div>
  23. <video id="video1" class="video-js vjs-default-skin vjs-big-play-centered" height="300" width="600" controls data-setup="{}">
  24. <source src="http://vjs.zencdn.net/v/oceans.mp4" type='video/mp4'>
  25. <source src="http://vjs.zencdn.net/v/oceans.webm" type='video/webm'>
  26. <source src="http://vjs.zencdn.net/v/oceans.ogv" type='video/ogg'>
  27. <p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that
  28. <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
  29. </video>
  30. <script src="videojs.hotkeys.js"></script>
  31. <script>
  32. // initialize the plugin
  33. videojs('video1').ready(function() {
  34. this.hotkeys({
  35. volumeStep: 0.1,
  36. seekStep: 5,
  37. enableMute: true,
  38. enableFullscreen: true,
  39. enableNumbers: false,
  40. enableVolumeScroll: true,
  41. enableHoverScroll: true,
  42. captureDocumentHotkeys: true,
  43. documentHotkeysFocusElementFilter: e => e.tagName.toLowerCase() === "body",
  44. // Mimic VLC seek behavior, and default to 5.
  45. seekStep: function(e) {
  46. if (e.ctrlKey && e.altKey) {
  47. return 5*60;
  48. } else if (e.ctrlKey) {
  49. return 60;
  50. } else if (e.altKey) {
  51. return 10;
  52. } else {
  53. return 5;
  54. }
  55. },
  56. // Enhance existing simple hotkey with a complex hotkey
  57. fullscreenKey: function(e) {
  58. // fullscreen with the F key or Ctrl+Enter
  59. return ((e.which === 70) || (e.ctrlKey && e.which === 13));
  60. },
  61. // Custom Keys
  62. customKeys: {
  63. // Add new simple hotkey
  64. simpleKey: {
  65. key: function(e) {
  66. // Toggle something with S Key
  67. return (e.which === 83);
  68. },
  69. handler: function(player, options, e) {
  70. // Example
  71. if (player.paused()) {
  72. player.play();
  73. } else {
  74. player.pause();
  75. }
  76. }
  77. },
  78. // Add new complex hotkey
  79. complexKey: {
  80. key: function(e) {
  81. // Toggle something with CTRL + D Key
  82. return (e.ctrlKey && e.which === 68);
  83. },
  84. handler: function(player, options, event) {
  85. // Example
  86. if (options.enableMute) {
  87. player.muted(!player.muted());
  88. }
  89. }
  90. },
  91. // Override number keys example from https://github.com/ctd1500/videojs-hotkeys/pull/36
  92. numbersKey: {
  93. key: function(event) {
  94. // Override number keys
  95. return ((event.which > 47 && event.which < 59) || (event.which > 95 && event.which < 106));
  96. },
  97. handler: function(player, options, event) {
  98. // Do not handle if enableModifiersForNumbers set to false and keys are Ctrl, Cmd or Alt
  99. if (options.enableModifiersForNumbers || !(event.metaKey || event.ctrlKey || event.altKey)) {
  100. var sub = 48;
  101. if (event.which > 95) {
  102. sub = 96;
  103. }
  104. var number = event.which - sub;
  105. player.currentTime(player.duration() * number * 0.1);
  106. }
  107. }
  108. },
  109. emptyHotkey: {
  110. // Empty
  111. },
  112. withoutKey: {
  113. handler: function(player, options, event) {
  114. console.log('withoutKey handler');
  115. }
  116. },
  117. withoutHandler: {
  118. key: function(e) {
  119. return true;
  120. }
  121. },
  122. malformedKey: {
  123. key: function() {
  124. console.log('I have a malformed customKey. The Key function must return a boolean.');
  125. },
  126. handler: function(player, options, event) {
  127. //Empty
  128. }
  129. }
  130. }
  131. });
  132. });
  133. </script>
  134. </body>
  135. </html>