CaretEvent: how to obtain the pressed key?


When listening to caret changes, in a CaretListner, how can we obtain
the pressed key that triggered the CaretEvent?
CaretEvent only provides position change and source - ? - information.
I need to find out which key - and special key - was pressed

Alain

16 comments

Is it possible?

I'm using a CaretListener to emulate FoldingRegion listener, and detect
leaving such a watched area, to refresh the camouflage text if necessary.
As some keys change the contents, or even the length of the region, I
need to know the key that triggered the caretEvent.

Alain

0

On 11/27/03 1:51 PM, in article bq4kvm$r9s$2@is.intellij.net, "Alain Ravet"
<alain.ravet.list@wanadoo.be> wrote:

No that is not possible. CaretListener only provides info of caret
position/offset changed that may happen even if user haven't taken any
actions. For example when file get saved trailing whitespaces are truncated
so caret changes its offset to appear at the same logical position.

--
Maxim Shafirov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"


0

Sorry to insist, but I'm stuck on this one.
I used reflection to examine the caretEvent, and its source (a
'com.intellij.openapi.editor.d.m' object !)'s methods and fields, to no
avail.

I'm sure this is 1c question, but I have no clue.

TIA
Alain

0

Maxim ,

>No that is not possible.

>

I'm not married to caretListener, so any way to capture the key pressed
by the user is good for me.
So, how can I listen to key pressed in an editor, and obtain the value
of the key ?

Alain

0

So, how can I listen to key pressed in an editor, and obtain the value
of the key ?


Do you want to listen for editor typing or in some control shortcuts?

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"


"Alain Ravet" <alain.ravet.list@wanadoo.be> wrote in message
news:bq51q5$ba4$2@is.intellij.net...

Maxim ,

>

>No that is not possible.
>
>

>

I'm not married to caretListener, so any way to capture the key pressed
by the user is good for me.
So, how can I listen to key pressed in an editor, and obtain the value
of the key ?

>

Alain

>


0

Valentin ,

>>So, how can I listen to key pressed in an editor, and obtain the value
>>of the key ?
>>
>Do you want to listen for editor typing or in some control shortcuts?
>


I need to detect :
a/ when the caret enters a folding region (done)
b/ when the caret leaves a folding region (done)
and
c/ when the user has modified the folding region contents.


To solve c/, the easiest way I can think of would be to examine the key
- if any - that triggered the latest caret change. If it's a cursor move
the contents was not changed; otherwise, ...
When the caret change is not due to a user key-press -ex: mouse click -,
null would be returned as a key value.


Ideally, I'd like to see


added to the CaretEvent interface?

Does it make sense?

Alain

0

I think you just need to call getContentComponent() on the Editor,
and add a KeyListener.

The EditorFactoryListener or FileEditorManagerListener can be used to determine
when an editor is created or disposed; the problem I run into is associating
a Project with an Editor, but perhaps this is not your problem.

Alain Ravet wrote:

Sorry to insist, but I'm stuck on this one.
I used reflection to examine the caretEvent, and its source (a
'com.intellij.openapi.editor.d.m' object !)'s methods and fields, to no
avail.

I'm sure this is 1c question, but I have no clue.

TIA
Alain


--

Erb

==============================================================
"Most of you are familiar with the virtues of a programmer.
There are three, of course: laziness, impatience, and hubris."
- Larry Wall
==============================================================

0

On 12/1/03 6:57 PM, in article bqfodt$f04$1@is.intellij.net, "Erb"
<dont@evenbother.com> wrote:

I think you just need to call getContentComponent() on the Editor,
and add a KeyListener.

The EditorFactoryListener or FileEditorManagerListener can be used to
determine
when an editor is created or disposed; the problem I run into is associating
a Project with an Editor, but perhaps this is not your problem.

Alain Ravet wrote:

>> Sorry to insist, but I'm stuck on this one.
>> I used reflection to examine the caretEvent, and its source (a
>> 'com.intellij.openapi.editor.d.m' object !)'s methods and fields, to no
>> avail.
>>
>> I'm sure this is 1c question, but I have no clue.
>>
>> TIA
>> Alain
>>
This not gonna work. I've answered Alain personally (hit reply instead of
reply to newsgroup by mistake).
--
Maxim Shafirov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"


0

Maxim Shafirov (JetBrains) wrote:

>"Erb" wrote:
>
>>>...
>>>
>>>
>This not gonna work. I've answered Alain personally (hit reply instead of
>reply to newsgroup by mistake).

>


Maxim, thanks for your previous suggestion, but
- it's soooo heavy :intercepting every single editor action that can
modify the editor contents ,
and I think (I didn't test it, though) that :
- it wouldn't work with plugins that modify directly the editor
contents, like the ComponentMover and the LineUpAndDownMover plugins.


In a reply to Valentin, today, in this thread, I summarized my need :

Alain Ravet wrote:

I need to detect :
a/ when the caret enters a folding region (done)
b/ when the caret leaves a folding region (done)
and
c/ when the user has modified the folding region contents.

>
>

To solve c/, the easiest way I can think of would be to examine the
key - if any - that triggered the latest caret change. If it's a
cursor move the contents was not changed; otherwise, ...
When the caret change is not due to a user key-press -ex: mouse click
-, null would be returned as a key value.

>
>

Ideally, I'd like to see

   public SomeKeyType keypressed ()
> ]]>

added to the CaretEvent interface



Alain

0

Based on what you say, a KeyListener would not do the trick either, because of programmatic changes.

Hey, would a DocumentListener on the Document work? Perhaps you could combine this with the caret
listener to see if a caret change is accompanied by a document change? Hacky, but possible...

Alain Ravet wrote:

Maxim Shafirov (JetBrains) wrote:

>> "Erb" wrote:
>>
>>>> ...
>>>>
>>
>> This not gonna work. I've answered Alain personally (hit reply instead of
>> reply to newsgroup by mistake).
>>
>>


Maxim, thanks for your previous suggestion, but
- it's soooo heavy :intercepting every single editor action that can
modify the editor contents ,
and I think (I didn't test it, though) that :
- it wouldn't work with plugins that modify directly the editor
contents, like the ComponentMover and the LineUpAndDownMover plugins.


In a reply to Valentin, today, in this thread, I summarized my need :

Alain Ravet wrote:

>> I need to detect :
>> a/ when the caret enters a folding region (done)
>> b/ when the caret leaves a folding region (done)
>> and
>> c/ when the user has modified the folding region contents.
>>
>>
>> To solve c/, the easiest way I can think of would be to examine the
>> key - if any - that triggered the latest caret change. If it's a
>> cursor move the contents was not changed; otherwise, ...
>> When the caret change is not due to a user key-press -ex: mouse click
>> -, null would be returned as a key value.
>>
>>
>> Ideally, I'd like to see
>>

>   public SomeKeyType keypressed ()
>> ]]>

added to the CaretEvent interface



Alain


--

Erb

==============================================================
"Most of you are familiar with the virtues of a programmer.
There are three, of course: laziness, impatience, and hubris."
- Larry Wall
==============================================================

0

Erb wrote:

Based on what you say, a KeyListener would not do the trick either,
because of programmatic changes.

>

Hey, would a DocumentListener on the Document work? Perhaps you could
combine this with the caret
listener to see if a caret change is accompanied by a document
change? Hacky, but possible...




Immediate thought : as there is no - documented - guarantee about the
firing order of those 2 listeners, I don't see how to program this!

Do you ?


Alain

0

I'm just a cowboy programmer, I guess...
The issue is not only order, but timing. I'd look for:
1) a certain maximum time between events, and 2) change occurring at
matching locations.
In the dream situation, the caret change would occur between the beforeDocumentChange call
and the documentChanged call. At any rate, since we've pretty much had to update our plugins
for every release of IDEA (due to API changes), I'd just make it work in Aurora and then
worry about later versions when they occur. This sort of behavior may change, but only
until the release of Aurora, and until then we have to keep changing our plugins anyway.

Alain Ravet wrote:

Erb wrote:

>> Based on what you say, a KeyListener would not do the trick either,
>> because of programmatic changes.
>>
>> Hey, would a DocumentListener on the Document work? Perhaps you could
>> combine this with the caret
>> listener to see if a caret change is accompanied by a document
>> change? Hacky, but possible...




Immediate thought : as there is no - documented - guarantee about the
firing order of those 2 listeners, I don't see how to program this!

Do you ?


Alain


--

Erb

==============================================================
"Most of you are familiar with the virtues of a programmer.
There are three, of course: laziness, impatience, and hubris."
- Larry Wall
==============================================================

0

Alain Ravet wrote:

>> Ideally, I'd like to see
>>

>   public SomeKeyType keypressed ()
>> ]]>

added to the CaretEvent interface
>


It wouldn't work with : the caret doesn't move, but the text has
changed.

So, it looks like I need another solution ?

Alain

0

c/ when the user has modified the folding region contents.


Why isn't DocumentListener suitable for this?

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"


"Alain Ravet" <alain.ravet.list@wanadoo.be> wrote in message
news:bqfbdl$h2h$2@is.intellij.net...

Valentin ,

>

>>So, how can I listen to key pressed in an editor, and obtain the value
>>of the key ?
>>
>Do you want to listen for editor typing or in some control shortcuts?
>

>
>

I need to detect :
a/ when the caret enters a folding region (done)
b/ when the caret leaves a folding region (done)
and
c/ when the user has modified the folding region contents.

>
>

To solve c/, the easiest way I can think of would be to examine the key
- if any - that triggered the latest caret change. If it's a cursor move
the contents was not changed; otherwise, ...
When the caret change is not due to a user key-press -ex: mouse click -,
null would be returned as a key value.

>
>

Ideally, I'd like to see

    public SomeKeyType keypressed ()
> ]]>

added to the CaretEvent interface?

>

Does it make sense?

>

Alain



0

Valentin Kipiatkov (JetBrains) wrote:

>> c/ when the user has modified the folding region contents.
>>
>
>Why isn't DocumentListener suitable for this?
>
>

Because of a) and b), that are only served by the CaretListener :
a/ detect when the caret enters a folding region (done)
b/ detect when the caret leaves a folding region (done)



I'd much prefer to do all in 1 place (1 thread, 1 listener, 1 class).



Alain

0

I'd much prefer to do all in 1 place (1 thread, 1 listener, 1 class).


Well changing text and moving caret is logically 2 different things. It's
strange to expect them to be handled by one listener.
As about "1 thread" both changing text and moving caret events may come only
from event dispatch thread.

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"

"Alain Ravet" <alain.ravet.list@wanadoo.be> wrote in message
news:bqkudg$mbo$3@is.intellij.net...

Valentin Kipiatkov (JetBrains) wrote:

>

>> c/ when the user has modified the folding region contents.
>>
>
>Why isn't DocumentListener suitable for this?
>
>

>

Because of a) and b), that are only served by the CaretListener :
a/ detect when the caret enters a folding region (done)
b/ detect when the caret leaves a folding region (done)

>
>
>

I'd much prefer to do all in 1 place (1 thread, 1 listener, 1 class).

>
>
>

Alain



0

Please sign in to leave a comment.