PDA

View Full Version : How to prevent focus on button after mouse click...



Wizard
14-12-2007, 10:21 AM
Hi, I have two buttons with onClick events on my form as well as a component that receives keyboard input. Problem is that when the left, right, up and down cursors are pressed focus jumps to and from the buttons and when the spacebar is pressed it performs the same function as the mouse click -- which I do not need :evil:

How can I prevent delphi from moving focus to the buttons?

User137
14-12-2007, 12:27 PM
Try setting button TabStop property to false.

Wizard
14-12-2007, 12:37 PM
The tabstop property is set to false, I even have dxDraw.setfocus. I did a lot of tests with all the gui properties of the button and the form and no joy.

TabStop doesn't do a thing for my problem, do a simple test and place 3 buttons on a form -- try to move between them with the cursor keys on your keyboard, it's not moving focus right? That's all good and well but click on one of them (even without onclick event handler) and move the cursor keys.....No tabstop anymore... TabStop only works if you press Tab, not if the user clicks and then press the cursor keys.

I want to stop the moving between the controls if a mouse click happens and the user then press the cursor keys. In my game the mouse click on a button will enable/disable music and directly after that the user must select (with the cursor keys) a further item to play or to close.

I'm using 2 buttons on dxDraw but I don't think DelphiX has anything to do with the tab behaviour of the buttons.

User137
14-12-2007, 03:51 PM
I've had that problem long time ago. Then another thing to try:
form.Setfocus;
Because DXDraw isn't something to give focus to if i recall.

Bones
14-12-2007, 03:51 PM
I want to stop the moving between the controls if a mouse click happens and the user then press the cursor keys. In my game the mouse click on a button will enable/disable music and directly after that the user must select (with the cursor keys) a further item to play or to close.


The simplest way to do this is is to just do a setfocus in the onClick event handler of each button eg.

procedure TForm1.Button1Click(Sender: TObject);
begin
Music_Off;
Form1.SetFocus;
end;

If you then disable the tabstop for the buttons to prevent them getting focus any other way, it should work nicely.
I tested this with a Tmemo component and three buttons, and I think it does what you want it to.


[edit]
scratch that, I just tested it with a dxDraw component, and it dont work, I think dxdraw wont take the input focus, because it doesn't handle input itself

Bones
14-12-2007, 04:10 PM
ok try this :

put a Tedit component on your form and resize it to 10x10

clear the text

switch off ctl3d for the edit box

Set the borderstyle = bsNone

set the color to the background color of your form (makes it invisible)

then in the onclick event of each button, do :

Edit1.setfocus;


make sure you switch off tabstop for all buttons and the editbox to stop the buttons getting focus accidentally.

I'm not sure if the editbox will prevent the dxinput from recieving messages but I shouldn't think so.

The only other way I know is to hook the windows message, but that way is a bit compicated.

tpascal
14-12-2007, 04:34 PM
finish your ONclick button event with this:

ActiveControl:=nil;

Bones
14-12-2007, 05:26 PM
finish your ONclick button event with this:

ActiveControl:=nil;

nice one.. thats an even better way.

Wizard
15-12-2007, 06:36 AM
Thanks for the replies guys :-)

ActiveControl := Nil works 100%, problem solved!