Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get pressed key onevent #91

Closed
DavidSichau opened this issue Nov 2, 2016 · 10 comments
Closed

How to get pressed key onevent #91

DavidSichau opened this issue Nov 2, 2016 · 10 comments

Comments

@DavidSichau
Copy link

I would like to add the onevent listener to a form field, which should detect if the enter key is pressed.
Such that I am then able to submit the form.

However I therefore need to know which key was pressed. Is there a way to gain the code of the key pressed, such that I could make a check if it is the entered key.

I tried the following code, assuming that the handler somehow gets as parameters the pressed key:

shinyjs::onevent("keypress", 'passwd', (function(e) {
        print(e)
    })(e))

However unfortunately the pressed key is not passed and this code complains about not existent e. Is there another way to gain access to the pressed key?

@daattali
Copy link
Owner

daattali commented Nov 2, 2016

The onevent functions do not provide the javascript event back to R, they're only to be used for evaluating an R expression when an event happens. Usually when I need info about the event details, I just use javascript. But maybe I will try to support this, I'll open an issue and think about whether it's doable

@daattali
Copy link
Owner

daattali commented Nov 2, 2016

@DavidSichau if you install the dev version of shinyjs, you should now be able to use a function with an argument as the callback, and the argument will have the relevant javascript event details

@daattali
Copy link
Owner

daattali commented Nov 2, 2016

@DavidSichau it won't work with the code you tried (you probably wrote it like that because shinyjs expected an expression rather than a function). Just pass the function, without wrapping it in brackets

I'd appreciate if you try the new package and let me know if it works for your purposes

@DavidSichau
Copy link
Author

@daattali Thanks it works great. Unfortunately not on elements added by insertUI. So I will switch for them to js code. But for other cases it works.

@daattali
Copy link
Owner

daattali commented Nov 3, 2016

Hmm I don't see why it wouldn't work... as long as you're calling the
onevent function after the element is inserted, it should work (calling it
before the element exists won't work because the element doesn't exist yet
so there's nothing to attach to). Could you post a code sample so I can
look?
On Nov 3, 2016 10:14 AM, "David Sichau" [email protected] wrote:

@daattali https://github.com/daattali Thanks it works great.
Unfortunately not on elements added by insertUI. So I will switch for them
to js code. But for other cases it works.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#91 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA6IFL_Ft5zZtHu5eY5ISf1_4xEd_5KUks5q6ewvgaJpZM4KnAEA
.

@DavidSichau
Copy link
Author

DavidSichau commented Nov 4, 2016

@daattali
Copy link
Owner

daattali commented Nov 4, 2016

You have two problems with your code.

First problem is that you're not including shinyjs in your UI (you MUST include a call use useShinyjs() in the UI in order for shinyjs functions to work!)

Second problem is what I mentioned in an above comment "it won't work with the code you tried ... Just pass the function, without wrapping it in brackets"

Instead of

      shinyjs::onevent("keypress", 'passwd', (function(e) {
        print(e)
      })(e))

It should be

      shinyjs::onevent("keypress", 'passwd', function(e) {
        print(e)
      })

The first one doesn't work because you're passing an expression that's trying to access a non existent variable e, you need to pass a function instead that will only get evaluated later

@DavidSichau
Copy link
Author

@daattali Thanks for your reply. I think the problem was that I forgot to add useShinyjs() in the client and then the rest got a mess :(

Now the problem is solved 👍

@DavidSichau
Copy link
Author

@daattali If I put the code inside a module it somehow does not work anymore:

https://gist.github.com/DavidSichau/5c7bb50d4dcac68621e6b69ae9c6119d

Did I again made a stupid error or is this not possible?

@daattali
Copy link
Owner

daattali commented Nov 4, 2016

That's not possible from the simple fact that what you're doing there is putting the onevent call inside UI. If you look at the documentation for showModal(), you'll see that the argument it takes is ui code. I hope that clarifies it for you.

If you have any bug reports you're welcome to open new issues, but I prefer that any conversation about "how do I do this/is this a bug or not?" be done on StackOverflow (that way it's easy for other people to see it in the future and other people can help me with answering questions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants