KeyListener, how to know which key has been pressed

Hello all.

I want to capture the [Enter] keyPressed event into a field with the KeyListener Plugin.

My OnLoad function …

function OnLoad(){

plugins.keyListeners.addKeyListener(elements.myField, keyListener, 'keyPressed');

}

My callback function …

function keyListener(){

...... if  [Enter Key] ... then do something ....

}

How can I know if Enter Key has been pressed?

Thanks in advance.

Hi,

the event of the call back function holds the key pressed, enter is passed as “\n” (at least in svy 5.2.9)

Regards,

Peter

Hi, Roberto.

Something like this may work in keyListener():

if ( event.getKeyChar() == numericValueForEnterKey )
{
    // Process ENTER key    
}

Peter de Groot, kwpsd

Thanks a lot for your replies :-)

Best regards. Roberto Blasco.

Hello,

i want to capture [TAB] but i don’t know how can i do it, can someone help me?

and second, i want to print the properties of the pressed key…is it possible?

Thanks

TAB is event.getKeyChar() === 9

The event that is passed back to your callback function is of a Java object: java.awt.event.KeyEvent.

Have a look at the KeyEvent javadocs to see what kind of methods are available: http://docs.oracle.com/javase/6/docs/ap … Event.html

I have this code on my form:

function onLoad(){
	plugins.keyListeners.addKeyListener(elements.listenedField, keyListener, 'keyPressed');
}

and

function keyListener(){
    [...] my code [...]
}

but i can’t use event.getKeyChar() because i haven’t got it

If keyListener is your callback function, it’s signature should be:

function keyListener(event) {
    if (event.getKeyChar() === 9) {
        // do something
    }
}

Thanks a lot! Now it works perfectly!

Is there any possibility to intercept the ARROW Up/Down?

If you go to KeyEvent (Java Platform SE 6),
in Field Summary section there’s all the constants for the keys that you can use like

if (event.getKeyChar() == Packages.java.awt.event.KeyEvent.VK_UP)
// do something

If you want to use directly the correspondent number:

//did i need to set something particolar to my textfield because it doesn’t work with arrows?

i set the wrong type on the plugin…now it works

plugins.keyListeners.addKeyListener(elements.listenedField, keyListener, 'keyPressed');

i need to intercept the TAB with two or more textfields,
i tried with

plugins.keyListeners.addKeyListener(elements.textField_reparto,keyListener,'keyPressed')

but the TAB is not intercepted

any ideas?

has anybody here got this plugin to work?
if yes, could you please post a complete sample of your code?
I tried all the ways explained in this post but I still can’t get the callback function working…

Hi,

studiomiazzo:
has anybody here got this plugin to work?
if yes, could you please post a complete sample of your code?
I tried all the ways explained in this post but I still can’t get the callback function working…

I have got it to work, using the sample solution that is supplied with it (I don’t know about the code in this post working or not, but sample solution does)
make sure you download the sample on the files page (Files - KeyListeners Plugin - ServoyForge) as well and import that and have a play
If you still can’t get it to work, reply again…
Rafi

I’d tried the sample solution in the meanwhile, it works but only in the smart client…
Did you ever try it with the web client?
I suppose that I cannot get the typed key in this manner when I’m on the web…

studiomiazzo:
I’d tried the sample solution in the meanwhile, it works but only in the smart client…
Did you ever try it with the web client?
I suppose that I cannot get the typed key in this manner when I’m on the web…

It does state on the wiki page (Wiki - KeyListeners Plugin - ServoyForge)

Currently not web compatible, as it makes use of Swing Component listeners.
Not sure if I will make try to make a Wicket (web client) behavior for this one, since it would make Ajax call to the server for each key typed in the “listened” field, which could take your server down after a while…
Will see if I can use some Throttle behavior to implement this safely.
For now in the web client it will not break though, it will just do nothing and tell you so in the server log.

so, no, it won’t work on web client (I’d like it if it did too :) )

:wink:

I have to read more carefully documentation!!!
Thx for your help!

:D

studiomiazzo:
:wink:
I have to read more carefully documentation!!!
Thx for your help!