I am facing a strange issue. I have set of buttons in a panel and I want to show tooltip for each button. For that I am using TPopUp, but whenever mouse enter, I can observe that memory is increasing for the application. But if I comment the mouse enter and mouse leave events then memory doesn't increase. Did I miss something?
Whenever the mouse enters the button, I can see 0.3MB increase in my task manager.
TfrmEncode = class(TForm)
pnlTop: TPanel;
btnSaveToJSON: TButton;
procedure FormCreate(Sender: TObject);
procedure btnSaveToJSONMouseEnter(Sender: TObject);
procedure btnSaveToJSONMouseLeave(Sender: TObject);
private
{ Private declarations }
pop : TPopup;
cb : TColorBox;
labelText: TLabel;
public
{ Public declarations }
end;
implementation
{$R *.fmx}
procedure TfrmEncode.btnSaveToJSONMouseEnter(Sender: TObject);
begin
Pop.IsOpen := True;
end;
procedure TfrmEncode.btnSaveToJSONMouseLeave(Sender: TObject);
begin
Pop.IsOpen := False;
end;
procedure TfrmEncode.FormCreate(Sender: TObject);
begin
try
pop := TPopup.Create(self);
pop.Parent:= self;
pop.Width:=200;
cb := TColorBox.Create(pop);
cb.Align := TAlignLayout.Client;
cb.Color := TAlphaColors.White;
pop.AddObject(cb);
labelText := TLabel.Create(pop);
labelText.Align :=TAlignLayout.alClient;
labelText.Parent := pop;
labelText.Text := 'This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint This is the hint';
pop.AddObject(labelText);
pop.PlacementTarget := btnSaveToJSON;
pop.Placement:=TPlacement.BottomCenter;
finally
end;
end;
procedure TfrmEncode.FormDestroy(Sender: TObject);
begin
FreeAndNil(pop);
end;
There is a bug in TPopup control. Reported as RSP-21438
TPopup internally creates new TCustomPopupForm every time popup is open. However, that form does not get released when popup is closed (as it should) but only when popup control itself is destroyed.
There are few workarounds
1. Create new TPopup control on open and free it on close
2. Fix FMX.Controls and FMX.Forms
Error can be fixed in implementation section of the above units. That means you can copy FMX.Controls and FMX.Forms into your project folder and Delphi will use those fixed units instead of default ones.
Fix following code:
FMX.Controls - change constructor parameter from False to True - it means popup form will be automatically released on close.
function TPopup.CreatePopupForm: TFmxObject;
...
NewForm := TCustomPopupForm.Create(Self, NewStyle, PlacementTarget, True);
FMX.Forms - assign AutoFree parameter to field.
constructor TCustomPopupForm.Create(AOwner: TComponent; AStyleBook: TStyleBook = nil; APlacementTarget: TControl = nil;
AutoFree: Boolean = True);
var
NewStyleBook: TStyleBook;
begin
FAutoFree := AutoFree;
....
Delphi 10.2.2 mobile
Starting with a blank mobile project, I drop a TListBox on the form. I add two TListBoxItems.
procedure TForm1.ListBox1Click(Sender: TObject);
begin
ShowMessage('ListBoxItem.itemindex = ' + ListBox1.ItemIndex.ToString);
end;
When I click on the first item in Windows and Macintosh, the OnClick() correctly reports that item index 0 has been clicked.
When I click on the first item in mobile (iOS and Android) the OnClick() reports the item index as -1 (not 0 as it should). Then it goes on to highlight the first item.
If I then click on the second item in mobile, the OnClick() reports the item index as 0 (not 1 as it should). Then it goes on to highlight the second item.
How can I get the correct item in OnClick() when clicking in a TListBox on mobile?
Clearly the OnClick event is being triggered before the ItemIndex is updated. So you will have to delay processing until after the ItemIndex has a chance to be updated first. You can:
use TThread.ForceQueue() (10.2 Tokyo+ only):
procedure TForm1.ListBox1Click(Sender: TObject);
begin
TThread.ForceQueue(nil,
procedure
begin
ShowMessage('ListBoxItem.itemindex = ' + ListBox1.ItemIndex.ToString);
end
);
end;
use TThread.Queue():
procedure TForm1.ListBox1Click(Sender: TObject);
begin
TThread.CreateAnonymousThread(
procedure
begin
TThread.Queue(nil,
procedure
begin
ShowMessage('ListBoxItem.itemindex = ' + ListBox1.ItemIndex.ToString);
end
);
end
).Start;
end;
use a short timer:
procedure TForm1.ListBox1Click(Sender: TObject);
begin
Timer1.Enabled := True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
ShowMessage('ListBoxItem.itemindex = ' + ListBox1.ItemIndex.ToString);
end;
When I open a pdf document using the AcroPDF component, AcroPDF steals the focus of my edit component (edSuchName)...
the only way that I found to get it back is the following, but I'm not really glad with it. So I'm searching for something better
TMainForm = class(TForm)
AcroPDF1: TAcroPDF;
tmrBringFocusBack: TTimer;
edSearchName: TEdit;
procedure tmrBringFocusBackTimer(Sender: TObject);
procedure OpenDocument;
end;
// ...
procedure TMainform.tmrBringFocusBackTimer(Sender: TObject);
begin
tmrBringFocusBack.Enabled := False;
edSearchName.SetFocus;
end;
procedure TMainform.OpenDocument;
begin
AcroPDF1.LoadFile(AFilename);
AcroPDF1.setShowToolbar(false);
AcroPDF1.setPageMode('none');
AcroPDF1.Show;
tmrBringFocusBack.Interval:= 200; // and sometimes more (when trying, 20 is too less)
tmrBringFocusBack.Enabled := True; // <<<< I have to trigger this timer <<<<
end;
Does anybody know a better way?
Using the code below, or maybe modifying it, possible to achive my goal?
Or not by using this code, but it must be joystick buttons using when form is hidden in tray.
Thanks
type
TForm125 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
HotKey1 : Integer;
procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
public
end;
var
Form125: TForm125;
implementation
{$R *.dfm}
procedure TForm125.FormCreate(Sender: TObject);
begin
HotKey1 := GlobalAddAtom('MyAppHotkey1');//create a unique value for identify the hotkey
if not RegisterHotKey(Handle, HotKey1, MOD_CONTROL, VK_F1) then //register the hotkey CTRL + F1
ShowMessage('Sorry can not register the hotkey');
end;
procedure TForm125.FormDestroy(Sender: TObject);
begin
UnRegisterHotKey(Handle, HotKey1);//unregister the hotkey
GlobalDeleteAtom(HotKey1);//remove the atom
end;
procedure TForm125.WMHotKey(var Msg: TWMHotKey);
begin
if Msg.HotKey = HotKey1 then
ShowMessage('Hello'); // do your stuff
end;
Sorry, this is a follow up on Chris' answer, but it seems OP needs a little more assistance.
I also believe that the use of a joystick component is the way to go.
For example, NLDJoystick. The installation instructions are included, as well as a mini manual.
You will need to follow these steps:
Place the component on your form,
Set Active to True (this won't succeed when there is no joystick attached),
Implement the OnButtonDown event, as follows:
procedure TForm1.NLDJoystick1ButtonDown(Sender: TNLDJoystick;
const Buttons: TJoyButtons);
begin
Beep;
end;
The TJoyButtons type is a set of JoyBtn1..JoyBtn32, so if you wish you can react to a specific button, or a combination of multiple pressed buttons:
procedure TForm1.NLDJoystick1ButtonDown(Sender: TNLDJoystick;
const Buttons: TJoyButtons);
begin
if JoyBtn1 in Buttons then Beep;
//or:
if Buttons = [JoyBtn1, JoyBtn2] then Beep;
end;
Note that if Advanced is False (the default setting) that there are only 4 buttons supported.
You can check the state of the buttons of your joystick(s) when you need to check them... if works even if the form is hidden:
uses ..., MMSystem;
const
iJoystick = 1; // ID of the joystick
var
myjoy : TJoyInfoEx;
begin
myjoy.dwSize := SizeOf(myjoy);
myjoy.dwFlags := JOY_RETURNALL;
if (joyGetPosEx(iJoystick, #myjoy) = JOYERR_NOERROR) then
begin
if (myjoy.wbuttons and joy_button1) > 0 then // you can do it for all the buttons you need
begin
ShowMessage('button 1 down');
end;
end;
end;
Eventually, you can create a timer which often checks their status to know if the status has change and trigger what you need...
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I handle a keyboard shortcut when my program isn't active?
hello guys
i need to make a delphi program that creates a short cut key that works outside the delphi app. for example: when i press ctrl+1 it pastes a certain text. When i press ctrl+2, another text, and so on. It really helps my work. I managed to make a delphi application that does that, but it only works inside that app. i want it to work in all windows applications as long as my app is open ( and minimized). Can anyone help me out? I'm pretty new at delphi, i'm trying to learn.
I tried this code which someone recommended to me but it doesn't work. it does nothing. what did i do wrong?
unit Unit3;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Clipbrd;
type
TForm17 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
HotKey1 : Integer;
HotKey2 : Integer;
procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
public
{ Public declarations }
end;
var
Form17: TForm17;
implementation
{$R *.dfm}
{ TForm17 }
procedure TForm17.FormCreate(Sender: TObject);
const
MOD_CONTROL = $0002;//0x0002
begin
// Register Ctrl + 1 hotkey
HotKey1 := GlobalAddAtom('Hotkey1');
RegisterHotKey(Handle, HotKey1, MOD_CONTROL, Ord('1'));
// Register Ctrl + 2 hotkey
HotKey2 := GlobalAddAtom('Hotkey2');
RegisterHotKey(Handle, HotKey2, MOD_CONTROL, Ord('2'));
end;
procedure TForm17.FormDestroy(Sender: TObject);
begin
//unregister the hotkeys
UnRegisterHotKey(Handle, HotKey1);
GlobalDeleteAtom(HotKey1);
UnRegisterHotKey(Handle, HotKey2);
GlobalDeleteAtom(HotKey2);
end;
procedure TForm17.WMHotKey(var Msg: TWMHotKey);
begin
if Msg.HotKey = HotKey1 then
begin
ShowMessage('Ctrl + 1 was pressed');
Clipboard.AsText := 'This is my own text!';
end
else
if Msg.HotKey = HotKey2 then
begin
ShowMessage('Ctrl + 2 was pressed');
Clipboard.AsText := 'This is my own text!';
end;
end;
end.
You need to use RegisterHotKey and UnregisterHotKey from the Win32 API, they are very straightforward to use.
Also, you may find useful ShortCutToKey(), which returns the key code and shift state of a Delphi shortcut.
PS: Don't forget to check the return value of RegisterHotKey(), since it will fail if the hotkey is already registered by other application.
Edit: sorry, I though that you were using another WM_MESSAGE, since first you posted the code as plain text and I only scanned through it...
I think that the problem with your code is that your are using GlobalAddAtom for the ID key, but you only need to use an unique ID inside your app (the docs for the function say that you need to use GlobalAddAtom only for a shared DLL). Try using just this:
const
ID_HOTKEY1=0;
ID_HOTKEY2=1;
procedure TYourForm.FormCreate(Sender: TObject);
begin
if not RegisterHotKey(Handle,ID_HOTKEY1,MOD_CONTROL,Ord('1'))
then Application.MessageBox('Error registering hot key 1','Error',MB_ICONERROR);
if not RegisterHotKey(Handle,ID_HOTKEY2,MOD_CONTROL,Ord('2'))
then Application.MessageBox('Error registering hot key 2','Error',MB_ICONERROR);
end;
procedure TYourForm.FormDestroy(Sender: TObject);
begin
UnregisterHotKey(Handle,ID_HOTKEY1);
UnregisterHotKey(Handle,ID_HOTKEY2);
end;
procedure TYourForm.WMHotKey(var Msg: TWMHotKey);
begin
Application.MessageBox(PChar(IntToStr(Msg.HotKey)),'Hotkey ID',MB_OK);
end;
Also, the MOD_CONTROL and related constants are already defined by Delphi, you don't need to redefine them.
andrei, check this sample code to paste a text in a external application using a hotkey.
the code show two options
1) sending the Ctrl+V combination to the focused window
2) sending a WM_PASTE message
function GetFocusedHandle: THandle;
var
ActiveHWND : THandle;
FocusedThread : DWORD;
begin
Result:=0;
ActiveHWND := GetForegroundWindow;
FocusedThread := GetWindowThreadProcessID(ActiveHWND, nil) ;
try
if AttachThreadInput(GetCurrentThreadID, FocusedThread, true) then
Result := GetFocus;
finally
AttachThreadInput(GetCurrentThreadID, FocusedThread, false) ;
end;
end;
procedure TForm17.WMHotKey(var Msg: TWMHotKey);
var
FocusWindowHwnd : THandle;
begin
if Msg.HotKey = HotKey1 then //option 1
begin
Clipboard.AsText := 'Text from Ctrl + 1 Hotkey';//Assign the text to the clipboard
//send the Ctrl + V combination to the current focused window
keybd_event(VK_CONTROL, 0, 0, 0);
keybd_event(Ord('V'), 0, 0, 0);
end
else
if Msg.HotKey = HotKey2 then //option 2
begin
FocusWindowHwnd:=GetFocusedHandle; //get the handle to the focused window
if FocusWindowHwnd<>0 then
begin
Clipboard.AsText := 'Text from Ctrl + 2 Hotkey';//Assign the text to the clipboard
SendMessage(FocusWindowHwnd,WM_PASTE,0,0);//send the WM_PASTE message
end;
end;
end;