How to identify the usage of the arrow keys

Discussions about CSEntry
Post Reply
AriSilva
Posts: 600
Joined: July 22nd, 2016, 3:55 pm

How to identify the usage of the arrow keys

Post by AriSilva »

Hi folks,
Is there a way to know which arrow key the user pushed in the screen?
In other words, we have a right arrow to advance to the next question, and a left arrow to retrun to the previous field.
How do we know which one is pushed?
Best
Ari
Best
Ari
justinlakier
Posts: 156
Joined: November 21st, 2022, 4:41 pm

Re: How to identify the usage of the arrow keys

Post by justinlakier »

Hello,

Pushing the right arrow to advance is the same as advancing with the Enter key on Windows, or advancing through logic on Android. When advancing it will execute the postproc of the current field and then the preproc of the next field as normal. If pushing the left button it will go back to the previous field, executing the onfocus but NOT the preproc for that previous field. You can set variables in the preproc to tell whether a question was advanced to normally or not. Does this help answer your question?

Hope this helps,
Justin
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: How to identify the usage of the arrow keys

Post by Gregory Martin »

On Windows, you can also keep track of all keys:
numeric lastKeystroke;

function OnKey(keystroke)
    lastKeystroke = keystroke;
    exit keystroke;
end;
Then you can look at lastKeystroke to determine if the last pressed key was an Enter, left arrow, tab, right arrow, etc.
AriSilva
Posts: 600
Joined: July 22nd, 2016, 3:55 pm

Re: How to identify the usage of the arrow keys

Post by AriSilva »

Some questions about Greg's answer:
1. the OnKey(keystroke) works only on windows, not in android?
2. which are the codes for left key and right key?

And from Justin:
when you say "If pushing the left button it will go back to the previous field", what it the meaning of "previous" field?
. the field before in teh field list?
. the field where we used a skip to skip the the present field?
. the field where we used a move to move to the present field?

And last:
In android, is there a way to prevent the left field to be pushed? I know we can prevent to show both arrows, but I want to eliminate ONLY the left arrow.
Best
Ari
Best
Ari
AriSilva
Posts: 600
Joined: July 22nd, 2016, 3:55 pm

Re: How to identify the usage of the arrow keys

Post by AriSilva »

What I would like to have is a check to know if I got here using the left key, like:
if lastkey(leftkey) then
...
endif;
Best
Ari
emmayashri
Posts: 6
Joined: April 9th, 2024, 3:45 am
Location: India

Re: How to identify the usage of the arrow keys

Post by emmayashri »

Hey hi, you can use JavaScript event listener to detect keydown events. By checking the event.keyCode property, you can determine whether the user pressed the left or right arrow key. Then, you can execute the appropriate action in your application.

ex

Code: Select all

document.addEventListener("keydown", function(event) {
  switch(event.keyCode) {
    case 37:
      // Left arrow key pressed
      // Perform action to return to previous field
      break;
    case 39:
      // Right arrow key pressed
      // Perform action to advance to the next question
      break;
    // Add cases for other arrow keys if needed
  }
});
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: How to identify the usage of the arrow keys

Post by Gregory Martin »

Regarding the OnKey function, which does indeed work only on Windows, you can use the OnKey Character Map to determine the keystrokes for left, right, etc.:

https://www.csprousers.org/help/CSPro/o ... r_map.html
Post Reply