keyEvents.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // implement `key` events
  2. document.onkeydown = checkKey;
  3. urlParams = new URLSearchParams(window.location.search);
  4. // Utils informations
  5. var host = window.location.host
  6. var pathname = window.location.pathname
  7. var baseUrl = location.protocol + "//" + host + pathname
  8. var KEYCODE_Q = '81'
  9. var KEYCODE_ENTER = '13'
  10. var KEYCODE_LEFT_ARROW = '37'
  11. var KEYCODE_RIGHT_ARROW = '39'
  12. // get params if exists
  13. if (urlParams.has('scene')){
  14. var scene = urlParams.get('scene')
  15. var expe = urlParams.get('expe')
  16. }
  17. console.log(scene)
  18. console.log(expe)
  19. function checkKey(e) {
  20. e = e || window.event;
  21. if (e.keyCode == '81') {
  22. // `q` for quit expe
  23. console.log('`q` key is pressed')
  24. window.location = ''
  25. }
  26. else if (e.keyCode == '13') {
  27. // check if experience is begin
  28. if (!BEGIN_EXPE){
  29. // right arrow
  30. window.location = window.location.href + "&begin=true"
  31. }
  32. }
  33. else if (e.keyCode == '37' || e.keyCode == '39'){
  34. // only do something is experience is begin
  35. if (BEGIN_EXPE){
  36. var answer;
  37. // left arrow key
  38. if (e.keyCode == '37'){
  39. console.log('left arrow is pressed')
  40. answer = '1'
  41. }
  42. // right arrow key
  43. if (e.keyCode == '39'){
  44. console.log('right arrow is pressed')
  45. answer = '0'
  46. }
  47. var iteration = 0;
  48. // update of iteration if exists
  49. if (urlParams.has('iteration')){
  50. iteration = urlParams.get('iteration')
  51. // increment step
  52. iteration++;
  53. }
  54. var params = "?scene=" + scene + "&expe=" + expe + "&begin=true&iteration=" + iteration + "&answer=" + answer
  55. console.log(baseUrl)
  56. window.location = baseUrl + params
  57. }
  58. }
  59. }