Board index » delphi » Sequence of mouse and keyboard events when selecting cells in a drawgrid

Sequence of mouse and keyboard events when selecting cells in a drawgrid

I am developing an application which uses a DrawGrid to present the user with rows of data to be selected. The data is order lines ready for despatch, and is grouped under customer and ship to location headings.

I am using a Drawgrid with an oncelldraw event to create the data, with different highlighting for the different row types, and pulling the data from several lists of objects downloaded from our AS/400.

I need the user to be able to select multiple rows, within a shipto. These can be contiguous or non-contiguous.

So far, I have got it working with the cursor, page up/down, and home/end keys, in combination with shift and ctrl, to enable the user to navigate the grid, moving from shipto to shipto with page up/down, and selecting either single rows, complete shiptos or contiguous rows. I maintain my own array of selected rows and use the onkeydown event to save the key press data and the onselectcell event to carry out the actions depending on the key that was pressed. In processing the key in the onselectcell event, I set the selected row into the Row property of the grid, which also fires onselectcell, and also reposition the TopRow is needed.

I now want to add mouse selection, to enable the user to select single, contiguous and discontiguous rows, as in Explorer with single click, plus shift or ctrl. However, the events fire in a different order, which also depend on which key is pressed, and the onselectcell fires before the mousedown event, so I cannot identify which buttons the user pressed in the onselectcell event. I also need to distinguish between single click - to select; double click - to carry out an action, like ENTER; and rightclick - to display a popup menu.

I suspect that part of the answer lies in the onselectcell event always assuming that it need do nothing, unless the immediately preceding event was a keydown. This means it will ignore mouse clicks and any nested evocations from itself changing the Row property.

I should then be able to handle the mouse events in onmousedown, onclick and ondoubleclick events. Right click should be easy - it just fires mousedown, so i can check the the right button. However, single and double click are harder to distinguish. The sequence of events I get seems to be :

Left Click        Left Double Click      Right Click
----------        -----------------      -----------
selectcell        selectcell
mousedown         mousedown              mousedown
singleclick       singleclick
                  doubleclick
                  mousedown ?

How can I identify a single click, other than by the absence of the doubleclick. Also, cursor selection also fires the singleclick event.

regards

Chris

 

Re:Sequence of mouse and keyboard events when selecting cells in a drawgrid


"Chris Hill" <chris.h...@aphydraulics.com> skrev i en meddelelse
news:3d342841$1_1@dnews...

Quote
> I also need to distinguish between single click - to select;
> double click - to carry out an action, like ENTER;
> How can I identify a single click,
> other than by the absence of the doubleclick.

You are making it a lot more difficult than it really is.
The fact that a double click event is preceeded by a (single) click event
simply doesn't matter.
In the click event you select (or deselect) an item. Period.
In the double click event you carry out an action. Period.
Read it again.
--
Finn Tolderlund

Re:Sequence of mouse and keyboard events when selecting cells in a drawgrid


Quote
In article <3d342841$1_1@dnews>, Chris Hill wrote:
> I need the user to be able to select multiple rows, within a shipto. These can be
> contiguous or non-contiguous.

> So far, I have got it working with the cursor, page up/down, and home/end keys,
> in combination with shift and ctrl, to enable the user to navigate the grid,
> moving from shipto to shipto with page up/down, and selecting either single
> rows, complete shiptos or contiguous rows. I maintain my own array of selected
> rows and use the onkeydown event to save the key press data and the onselectcell
> event to carry out the actions depending on the key that was pressed.

I think that (doing the action in OnSelectCell) was not a good decision. You should
do all the selecting stuff in the OnKeyDown handler. It is no great task to figure
out, given the current Row of the grid and the key the user pressed, which rows
are to be selected, so mark them selected and then invalidate the rows to draw
them selected (see Tcustomgrid.InvalidateRow). This way you will not have a problem
extending this to mouse selection. You should do the mouse handling using
OnMouseDown, OnMOuseMove, OnMouseUp only, not OnClick. In OnMouseDown you can
check the Shift parameter for ssDouble to identify a mouse down due to a double click.

But of course any double click will *always* be preceeded by a single click, and
since you cannot see into the future you never know whether a single click will be
followed by a double click. So you have to make some design decisions: if a
double-click on a selection is supposed to trigger an action on that selection
a single-click on a selection must be a NOP, it cannot be used to modify the selection
in any way. You can modify the single-click behaviour according to modifier keys
pressed, however. So a Ctrl-Click would still unselect the clicked line if
selected, a Shift-Click would shrink the selection to the clicked line (with a
nice conundrum on whether to shrink from top or bottom...) but a click without
any modifier keys would do nothing when it lands inside a selection.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:Sequence of mouse and keyboard events when selecting cells in a drawgrid


"Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote:

Quote
>In article <3d342841$1_1@dnews>, Chris Hill wrote:
>> I need the user to be able to select multiple rows, within a shipto. These can be
>> contiguous or non-contiguous.

>> So far, I have got it working with the cursor, page up/down, and home/end keys,
>> in combination with shift and ctrl, to enable the user to navigate the grid,
>> moving from shipto to shipto with page up/down, and selecting either single
>> rows, complete shiptos or contiguous rows. I maintain my own array of selected
>> rows and use the onkeydown event to save the key press data and the onselectcell
>> event to carry out the actions depending on the key that was pressed.

>I think that (doing the action in OnSelectCell) was not a good decision. You should
>do all the selecting stuff in the OnKeyDown handler. It is no great task to figure
>out, given the current Row of the grid and the key the user pressed, which rows
>are to be selected, so mark them selected and then invalidate the rows to draw
>them selected (see Tcustomgrid.InvalidateRow). This way you will not have a problem
>extending this to mouse selection. You should do the mouse handling using
>OnMouseDown, OnMOuseMove, OnMouseUp only, not OnClick. In OnMouseDown you can
>check the Shift parameter for ssDouble to identify a mouse down due to a double click.

>But of course any double click will *always* be preceeded by a single click, and
>since you cannot see into the future you never know whether a single click will be
>followed by a double click. So you have to make some design decisions: if a
>double-click on a selection is supposed to trigger an action on that selection
>a single-click on a selection must be a NOP, it cannot be used to modify the selection
>in any way. You can modify the single-click behaviour according to modifier keys
>pressed, however. So a Ctrl-Click would still unselect the clicked line if
>selected, a Shift-Click would shrink the selection to the clicked line (with a
>nice conundrum on whether to shrink from top or bottom...) but a click without
>any modifier keys would do nothing when it lands inside a selection.

Peter

I take your point about doing the processing in the key and mouse down event handlers - it will make life slightly easier since the selectcell event triggers for other reasons.

I was actually trying to 'break' the Windows event model by having a double click NOT process the single click, ie. single click to select a row, then a later double click on another row to open a dialog WITHOUT selecting the new row. Looks like I can't do that and will have to stick to the conventional way.

My other point was that the sequence of events for the mouse and keyboard are different. One would expect the sequence to be :

User did something - key or mouse down and then up
System saw user action - keypress or click or dblclick
Control, eg Grid, took action - cell selected

This sequence is followed for keyboard events, but the mouse sequence is the reverse.

Is this a quirk of the Borland VCL or is it the way MS says it should be ?

Regards

Chris

Re:Sequence of mouse and keyboard events when selecting cells in a drawgrid


Quote
In article <3d35233a$1_2@dnews>, Chris Hill wrote:
> My other point was that the sequence of events for the mouse and
> keyboard are different. One would expect the sequence to be :

> User did something - key or mouse down and then up
> System saw user action - keypress or click or dblclick
> Control, eg Grid, took action - cell selected

> This sequence is followed for keyboard events, but the mouse sequence is
> the reverse.

> Is this a quirk of the Borland VCL or is it the way MS says it should be ?

It is the VCl that does that. The mouse handling is quite complex in Tcustomgrid
and the MouseDown method calls SelectCell before it calls the inherited MouseDown.
It is the latter that fires the OnMouseDown event. OnClick fires only when the
mouse goes up again, of course.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Other Threads