Solved

Shift state while dragging

Posted on 1998-09-05
20
637 Views
Last Modified: 2010-05-18
 How can I capture the shift state (or keyboard events) while dragging items from a ListView components? (if the CTRL is pushed, the drag cursor must contain a +)
  The OnDragOver event doesn’t bring the Shift:TShiftState variable (which exists in the OnMouseMove event), and I don’t know where disappears the keyboard message (either the parent window nor ListView component doesn’t receive the message).
0
Comment
Question by:biroadam
  • 8
  • 8
  • 2
  • +2
20 Comments
 
LVL 3

Expert Comment

by:mirek071497
ID: 1338735
You can use something like this :

state := GetAsyncKeyState( VK_SHIFT );
if state=0 then { no shift }
else {with shift }

you can't use ther VK_LSHIFT in win95 !
llok to the help about the GetAsyncKeyState
0
 

Expert Comment

by:elkiors
ID: 1338736
you can also use an OnKeyDown event handler

procedure TForm1.ListView1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  { Beep if the Shift key is pressed otherwise put the value
    of the key down into the caption of a label }
  { the control key reports value of 17 }
  if key=16 then beep else label1.Caption:=inttostr(key);
end;

hope this is of use

regards

elkiors
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338737
You can also use this.......

if ssLeft in Shift then
  ShowMessage('The shift is pressed');

Hope this is of any use also =)

Regards,
Viktor Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338738
Actually this is for the mouse...sorry,,,,,

//Vik
0
 

Author Comment

by:biroadam
ID: 1338739
To mirek: It’m not interested in scanning the keyboard, I HAVE TO RECEIVE A KEYBOARD MESSAGE (or an other kind of message) when CTRL is pressed. But the OnDragOver event handler is called ONLY FOR MOUSE EVENTS (contrary to the OnMouseMove which is called for some keyboard events too).

To elkiors: the OnKeyDown isn’t called while dragging an item


0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338740
This is what you should do....

In OnDragDrop() of the item...
  state := GetAsyncKeyState( VK_CONTROL );
  if state=0 then { ctrl not in shift }
    else {ctrl in shift };

Regards,
Viktor Ivanov
0
 

Author Comment

by:biroadam
ID: 1338741
I repeat again: I MUST BE NOTIFIED WHEN CTRL IS PRESSED!

The OnDragDrop enevt handler isn’t called when a key (in my case -- CTRL) is pressed, so if the user doesn’t move the mouse and press the CTRL button, nothing happens.

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338742
That's exactly what the procedure Mirek gave you and later on I did, is doing. it notifies you when the Ctrl button on the keyboard is down or not...if it is then just do you thing, if it's not just exit and wait until it is......

Regards,
Viktor Ivanov
0
 

Author Comment

by:biroadam
ID: 1338743
Dear Viktor,

  I’m tired explaining over and over again that I must be NOTIFIED when CTRL is pressed.
  If you really think that your code works, just try it.
  First, try in an Explorer window copying some files and you’ll see that a + sign will appear when you press the CTRL  key -- even if you do not move the mouse.
  Then try the same thing in your program: start dragging an item, then hold the mouse button pressed and do not move the mouse. Then press then CTRL key -- nothing will happen.
  Why? Becouse your code is in an OnDragOver event handler, and is called only when your program is NOTIFIED that the user moved the mouse while dragging and item, but your program ISN’T NOTIFIED when the user press the CTRL key.
  Your code only checks if the CTRL is pressed WHEN THE USER MOVED THE MOUSE -- as I said, not enough.

Adam
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338744
Ok, then.....you want me to create a project so I can show you how to do it...Ok, here is some code....

1)Create a new application
2)Put a button on the form without changing any names of controls...
3)Click on the button(not double click) and find the property DragMode... Set it to dmAutomatic
4)In the form's OnDragOver enter this code...

procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Source is TButton)and(GetAsyncKeyState(VK_CONTROL) <> 0);
end;

5) In OnDragDrop of the form enter this code...

procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  Canvas.TextOut(x,y,Button1.Caption);
end;

Regards,
Viktor Ivanov
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:biroadam
ID: 1338745
Dear Viktor,

  I really don't understand what's happening here. I have the feeling that you don't read or don't understand my questions and comments, and unfortunately it's not for the first time (there were two other questions that you answered or commented in totally wrong way: first time about the system grays, and second time about the ListView component).
  It's your business why are you doing this, but please, do not answer my questions if you are not sure about it, because the others -- who maybe know the answer -- will not answer it.
  If you want to discuss privately about this, send me a mail at biroadam@softhome.net

  About your last answer: please, read again my last comment, specially that part which describes what your program should do, and please observe that your sample program does something else.

Adam
0
 
LVL 20

Accepted Solution

by:
Madshi earned 30 total points
ID: 1338746
biroadam,

(1) Add this to your mainForm:

  private
    procedure AppMessage(var Msg: TMsg; var Handled: Boolean);

(2) And this to the implementation of your main unit:

procedure TMyMainForm.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  case Msg.Message of
    WM_KEYDOWN : caption:=caption+'+';
    WM_KEYUP   : caption:=caption+'-';
    else         inherited;
  end;
end;

procedure TMyMainForm.MyMainFormCreate(Sender: TObject);
begin  // OnCreate event handler
  Application.OnMessage:=AppMessage;
end;

Regards, Madshi.
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1338747
I think so you receiving bad answers because you don't say us what you exactly need !
look I just think so you can place my code to the dragover event, but you probably don't need this.
YOU mUST DESCRIBE TOTALLY WHAT YOU NEED !
tell me if you need install own handler only for the ctrl or shift keypress event or you need to create the drah which was described later ?
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338748
I'm sorry if I've answered what I didn't have to... at least I'm trying to and that's one big part of learning new stuff...so you can be as mad as you want, but if you've given us more detailed explanation I'd answered if I could.... Now as I read everything all over again (as you told me to) I think Madshi's answer is right....there is maybe something little that you need to add.....

public
    procedure AppMessage(var Msg: TMsg; var Handled: Boolean);

{implementation}

{$R *.DFM}

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  if (Msg.Message = WM_KEYDOWN)and(Msg.wParam = VK_CONTROL) then
    caption := caption+'Ctrl has been pressed'
  else if(Msg.Message = WM_KEYUP)and(Msg.wParam = VK_CONTROL) then
    caption := caption+'Ctrl has been released'
  else
    inherited;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage:=AppMessage;
end;

I hope this time I'm not wrong and hope I've read the comments right,,,

BTW Please supply a little more detailed and clearer explanation next time...

Regards,
Viktor Ivanov
0
 

Author Comment

by:biroadam
ID: 1338749
Madshi,

Thank you, it was a good answer.
The only problem is that I can’t refresh the drag cursor when the user press the CTRL key: after the DragCursor property is changed the drag cursor remains the same, and will be changed only after the user moves the mouse. If you have a suggestion, please send me to biroadam@softhome.net.

Adam
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338750
How about this???
procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
     begin
       if (Msg.Message = wm_keydown)and(Msg.wParam = vk_control)then
       begin
         caption := 'Ctrl has been pressed';
         Application.ProcessMessages;
       end;
     else
        if(Msg.Message = wm_keyup)and(Msg.wParam = vk_control) then
        begin
         caption := 'Ctrl has been released';
         Application.ProcessMessages;
        end;
     else
       Inherited;
     end;
0
 

Author Comment

by:biroadam
ID: 1338751
 To mirek: I always have the feeling that I explain too detailed my questions, and a person who is good enough for answer it doesn't need such a long explanation.
  My experience is that one who gives a wrong answer for the first time, won't answer after the long explanations (which, in my opinion, are unnecessary), simply because the misunderstandings are caused by the incomplete knowledge.
  It is possible that my first question wasn't clear enough, though a good programmer who already met the problem must have been understand it. But after a few comments it had to be unmistakable, and after the fourth wrong answer that was not even close about my problem, I didn't know what to say (there were pages of detailed explanations).

To vladika: sorry, I didn't want to hurt your feelings, but I lost my patience. However, I suggest that you should answer only if you understand completely the question (that means you understand every word from the question/comment). Probably I lost many answers because you were the fastest.

Best regards,
Adam

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338752
I don't see vladika in this conversation...

>I suggest that you should answer only if you understand completely
>the question
I though I understood it and that gives me the right to answer it. If you don't like the answer better give better explanations next time...
>though a good programmer who already met the problem must have
>been understand it.
I'm still learning and have much to learn, but it doesn't mean that a programmer has to meet the problem in order to be able to answer it... Keep that in mind... As a metter of fact no one is perfect :)

Regards,
Viktor Ivanov
0
 

Author Comment

by:biroadam
ID: 1338753
Sorry, I want to say viktornet, not vladika (vladika is one of those who have answered my question).
0
 

Author Comment

by:biroadam
ID: 1338754
Sorry, I intented to say viktornet, not vladika (vladika is one of those who have answered my question, and I think he is a very good programmer).
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now