Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enabling manual Copy and Paste for custom TEdit 1

Status
Not open for further replies.

Opieo

Programmer
Jul 26, 2006
454
GB
Okay, once again I find myself at the end of my rope after searching for a day and finding everything except the final piece of information I need.
I got a component called [blue]TNumEdit[/blue] that (go figure) accepts only numbers and is descended from TEdit.
[red]TNumEdit[/red]
It also has options for enabling or disabling whether negatives and decimals are allowed. It is really nice.
However, it doesn't have Copy or Paste enabled, since the C and V keys are on the ignore list.
I am trying to edit the component to enable just copying and pasting.

Here is the components [blue]OnKeyPress[/blue] event:
Code:
procedure TNumEdit.WhenKeyPress(Sender: TObject; var Key: Char);
var
  p : integer;
begin
case Key of
  '0'..'9'  : ;
  '.',','   : if AllowDec AND
                 (pos(DecimalSeparator,Text)=0)
                then  Key:=DecimalSeparator
                else  Key:=#0;
  #8        : ;
  #45       : if FAllowNeg then
                begin
                p:=SelStart;
                eValue:=-eValue;
                if eValue>0
                  then  SelStart:=p-1
                  else  SelStart:=p+1;
                Key:=#0;
                end;
  else        Key:=#0;
end;
end;

I have tried adding a small function to check the keyboard if Ctrl is clicked (also comes from [red]about.delphi.com[/red]):
Code:
function TNumEdit.CtrlDown : Boolean;
var
   State : TKeyboardState;
begin
   GetKeyboardState(State) ;
   Result := ((State[vk_Control] And 128) <> 0) ;
end;
So here is what I had added to the [blue]OnKeyPress[/blue] case statement:
Code:
  'v'       : begin
               ShowMessage ('v is pressed');
               if (CtrlDown = True) then
                begin
                 ShowMessage ('Ctrl is pressed');
                 PasteFromClipboard
                end
               else
                begin
                 ShowMessage ('Ctrl is NOT pressed');
                 Key:=#0;
                end;
              end;
  'c'       : if (CtrlDown = True) then
               CopyToClipboard
              else
               Key:=#0;
The [blue]ShowMessage[/blue]'s are my temporary error trapping.
I find that in my program, the [blue]TNumEdit[/blue] will detect when v or c is pressed perfectly fine, unless holding the control button down when you hit it. If Ctrl is held down, it never gets into the [blue]OnKeyPress[/blue] event handler (Ctrl only counts for [blue]OnKeyDown[/blue], not [blue]OnKeyPress[/blue] I know, which is why I think this is happening).

Okay, added an [blue]OnKeyDown[/blue] (fires before [blue]OnKeyPress[/blue]) to just to try to handle if they are copying or pasting with keyboard shortcuts (Ctrl-C & Ctrl-V).
Code:
procedure TNumEdit.WhenKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 case Key of
  'v'       : if (ssCtrl in Shift) then
               PasteFromClipboard
              else
               Key:=#0;
  'c'       : if (ssCtrl in Shift) then
               CopyToClipboard
              else
               Key:=#0;
 end;

end;
However, it won't build with that, and tells me there is an invalid type for the [blue]'v'[/blue] & [blue]'c'[/blue] (char vs word).

I am very very close, know where all the code goes either way. I just need somebody to help me either accept the [blue]'v'[/blue] & [blue]'c'[/blue] as what is pressed (have tried using [blue]#43[/blue] for c and such too there, no go) in the [blue]OnKeyDown[/blue] since they come in as word. OR if we could get it to register the C or V with [blue]OnKeyPress[/blue] since I yoinked that tiny function to check if Ctrl is pressed (that is assuming it works fine and dandy).
Anyone? =)

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Have you tried adding an ActionList to the app?

Actionslists will allow you to define many things including Cut/Copy/Paste. Double click the component and in the action editor create 'New Standard Action' and select TEditCopy,TEditPaste.
Then on your event TNumEdit.WhenKeyDown call EditCopy1.Execute and EditPaste.Execute for C & V;

Good Luck
Tony



 
Haven't tried this but CTRL-C is #3 and CTRL-V is #22. In your WhenKeyPress event case statement, let them through, just like '0'..'9'.
 
try:

case key of

ord('v') :...
ord('c') :...

or
case Chr(key) of
'v' :...
'c' :...

/Daddy


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
harebrain, those were the codes I was looking for yesterday (for half of my search) and was unable to find.
They solved my problem perfectly.
For future reference to others:
I was able to remove the [blue]OnKeyDown[/blue] handler, as well at the [green]CtrlDown[/green] function.
The code from above that was in the [blue]OnKeyPress[/blue] that was the [blue]'v' : ....[/blue] and [blue]'c' : .....[/blue] is now just:
Code:
  #3        : ;  // Enables Ctrl-C
  #22       : ;  // Enables Ctrl-V

Tony1, thank you for the input, but it looks like I won't be diving into action lists until later. They sound neat though and I will be trying them later =P


~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Glad I could finally help someone!

I must've looked at a hundred ASCII tables on the Web, half with pop-ups, redirection, ads, and all kinds of annoying baloney, but none with this information just right there. (If you Google for "ASCII chart," you'll get more than a million hits, but good luck finding what you need.)

So here's a shameless plug for Multi-Edit, the source of the info I passed on. Go for a test drive at
 
const
c_A = #1; // Ctrl+A = ^A
c_C = #3; // Ctrl+C = ^C
c_F = #6; // Ctrl+F = ^F
bs = #8; // BackSpace Key
c_N = #14; // Ctrl+N = ^N
c_T = #20; // Ctrl+T = ^T
c_V = #22; // Ctrl+V = ^V
c_X = #24; // Ctrl+X = ^X
c_Z = #26; // Ctrl+Z = ^Z
esc = #27; // Escape Key
c_bs = #127; // Ctrl+BS

Just a few control Keys more that might come handy,
for use in e.g. OnKeyPress;

I like to hold the above constants as global constants to be reused all through the program.

Remember always to stop processing keys by setting Key:=#0

Procedure TForm.Edit1KeyPress(Sender: TOBject; var Key:Char);
Begin
case Key of
c_A : begin Key:=#0; Edit1.SelectAll; end;
c_C : begin Key:=#0; CopyToClipBoard; end;
c_V : begin Key:=#0; PasteFromClipBoard; end;
end;
End;

If you put Key=#0 in an else statement or as the last command in the procedure, you won't be able to process the build in escape if you want to leap out without changing the value already there.

Most input-boxes have infact default Ctrl+A, Ctrl+C, Ctrl+V and Ctrl+X as default build in functions, as well as the Escape function, but when you put Key=#0 in an else clause, you'll shortcircut those build in functions, not allowing the keypress to be seen.

On the other hand if you want to process Ctrl+A yourself,
you have to stop key processing, setting Key:=#0, or it will fire twice resulting in no selection at all.

A case statement could also go like this:

case Key of
c_A,
c_C,
c_V,
c_X,
esc,
'0'..'9' :; // Do nothing but letting the above
// keystrokes pass through and
else Key:=#0; // stopping all other
end;

Happy programming

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top