Jump to content

How to make button react only on some key.


MadBoy
 Share

Recommended Posts

Hey,

I would like to have GUI on which i have button. I want that button to react only on some key and only that key or mouse click. I don't want it to react on ENTER or SPACE. I've managed to build gui with button and have focus on button and ENTER or SPACE sends command. I would like it to react only on lets say "$" (shift+4). I am aware of SetHotKey but that's not the issue here as i want it to only work if that GUI or and Window is on top and not everywhere.

Please help,

MadBoy

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

  • Moderators

#include <Misc.au3>
If (_IsPressed('A0') Or _IsPressed('A1')) And _IsPressed('34') Then
    ;Do Something
EndIf
Doesn't work?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Should I put it under $button_2 case ?

Well... let's think about that for a second... you say you only want it to click the button if they use Shift+4 ... well, if you put it under the button "Case (conditional statement)" then they have to click the button and press shift+4 at the same time... see anything wrong with that?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Is this an effect you are going for?

Global $fClickit, $Main, $Button
HotKeySet('+4', '_ClickButton')
$Main = GUICreate('Some GUI', 200, 100)
$Button = GUICtrlCreateButton('Some Button To Click', 10, 35, 180, 30)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case - 3
            Exit
        Case $Button
            If Not $fClickit Then
                MsgBox(64, 'Clicked', 'You clicked the button')
            Else
                $fClickit = Not $fClickit
                MsgBox(64, 'Clicked', 'You used a hotkey to click the button')
            EndIf
    EndSwitch
WEnd
Func _ClickButton()
    $fClickit = Not $fClickit
    ControlClick(HWnd($Main), '', $Button)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Cute Ron!!

lol

but good

8)

Thanks I think ;)

Here's another way (I don't like this so much, but it will let the keys pass if that's your intention (of course you could do that with hotkeyset as well))

#include <Misc.au3>
Global $fClickit, $Main, $Button
$Main = GUICreate('Some GUI', 200, 100)
$Button = GUICtrlCreateButton('Some Button To Click', 10, 35, 180, 30)
GUISetState()
AdlibEnable('_ClickButton', 10)
While 1
    Switch GUIGetMsg()
        Case - 3
            Exit
        Case $Button
            If Not $fClickit Then
                MsgBox(64, 'Clicked', 'You clicked the button')
            Else
                $fClickit = Not $fClickit
                MsgBox(64, 'Clicked', 'You used a hotkey to click the button')
            EndIf
    EndSwitch
WEnd
Func _ClickButton()
    If (_IsPressed('A0') Or _IsPressed('A1')) And _IsPressed('34') Then
        $fClickit = Not $fClickit
        ControlClick(HWnd($Main), '', $Button)
        Sleep(100)
    EndIf
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks I think ;)

Here's another way (I don't like this so much, but it will let the keys pass if that's your intention (of course you could do that with hotkeyset as well))

#include <Misc.au3>
Global $fClickit, $Main, $Button
$Main = GUICreate('Some GUI', 200, 100)
$Button = GUICtrlCreateButton('Some Button To Click', 10, 35, 180, 30)
GUISetState()
AdlibEnable('_ClickButton', 10)
While 1
    Switch GUIGetMsg()
        Case - 3
            Exit
        Case $Button
            If Not $fClickit Then
                MsgBox(64, 'Clicked', 'You clicked the button')
            Else
                $fClickit = Not $fClickit
                MsgBox(64, 'Clicked', 'You used a hotkey to click the button')
            EndIf
    EndSwitch
WEnd
Func _ClickButton()
    If (_IsPressed('A0') Or _IsPressed('A1')) And _IsPressed('34') Then
        $fClickit = Not $fClickit
        ControlClick(HWnd($Main), '', $Button)
        Sleep(100)
    EndIf
EndFunc
Almost. I don't want space/enter working on button and when i do that on your code those still work :lmao: TNX for helping btw.

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Almost. I don't want space/enter working on button and when i do that on your code those still work ;) TNX for helping btw.

It works when button gets the focus on it..

Edit: but actually when you click the button the gui goes hidden so it is working in some way.. wonder if i will hide the gui after click and then show it again will the focus be on the button or will it be 'reset' in some way.

Edited by MadBoy

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

  • Moderators

My example hid on you? Or your implementation into your own script hid on you?

Global $Main, $Button
HotKeySet('+4', '_ClickButton')
$Main = GUICreate('Some GUI', 200, 100)
$Button = GUICtrlCreateButton('Some Button To Click', 10, 35, 180, 30)
GUISetState()
GUIRegisterMsg(0x0111, "_MY_WM_COMMAND")
While 1
    Switch GUIGetMsg()
        Case - 3
            Exit
    EndSwitch
WEnd
Func _MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0xFFFF), $hCtrl = $lParam
    If $nID = $Button And Not _IsPressed('0D') And Not _IsPressed('20') Then
        MsgBox(64, 'Clicked', 'You clicked the button')
    EndIf
    Return 'GUI_RUNDEFMSG'
EndFunc
Func _ClickButton()
    ControlClick(HWnd($Main), '', $Button)
EndFunc
Func _IsPressed($v_R, $v_dll = 'user32.dll')
    $v_R = DllCall($v_dll, 'int', 'GetAsyncKeyState', 'int', '0x' & $v_R)
    Return (Not @error And BitAND($v_R[0], 0x8000) = 0x8000) * 1
EndFunc   ;==>_IsPressed

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I just did quick test ;)

Global $fClickit, $Main, $Button
HotKeySet('+4', '_ClickButton')
HotKeySet('+5', '_Test')
$Main = GUICreate('Some GUI', 200, 100)
$Button = GUICtrlCreateButton('Some Button To Click', 10, 35, 180, 30)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case - 3
            Exit
        Case $Button
            If Not $fClickit Then
                MsgBox(64, 'Clicked', 'You clicked the button')
                GUISetState(@SW_HIDE, $Main)
            Else
                $fClickit = Not $fClickit
                MsgBox(64, 'Clicked', 'You used a hotkey to click the button')
                GUISetState(@SW_HIDE, $Main)
            EndIf
    EndSwitch
WEnd
Func _ClickButton()
    $fClickit = Not $fClickit
    ControlClick(HWnd($Main), '', $Button)
EndFunc


Func _Test()
    GUISetState(@SW_SHOW, $Main)
EndFunc

if you bring it back after pressing it once .. you can use space / enter then :lmao: lemme test your newest code :evil:

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

  • Moderators

Nice, space seems disabled for that window, just Enter still works ;) in your code that is :lmao:

Neither space nor enter worked when I tested the example I provided.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Neither space nor enter worked when I tested the example I provided.

Space works, Enter is disabled (not like i said in last post.)

i started your program

pressed SHIFT+4

pressed space and it worked ;) after shit+4

it even works here if i press space right away ;D if it realy doesn't work for you then i don't know whats up

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

  • Moderators

Yeah, retested...

Here's my last attempt, I noticed that I'm the only one trying anything here ;)

Global $Main, $Button
HotKeySet('+4', '_ClickButton')
HotKeySet('{ENTER}', '_BlockKey')
HotKeySet('{NUMPADENTER}', '_BlockKey')
HotKeySet('{SPACE}', '_BlockKey')
$Main = GUICreate('Some GUI', 200, 100)
$Button = GUICtrlCreateButton('Some Button To Click', 10, 35, 180, 30)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case - 3
            Exit
        Case $Button
            MsgBox(64, 'Clicked', 'You clicked the button')
    EndSwitch
WEnd
Func _BlockKey()
    Switch @HotKeyPressed
        Case '{ENTER}'
            If ControlGetFocus(HWnd($Main)) <> 'Button1' Then
                HotKeySet('{ENTER}')
                Send('{ENTER}')
                HotKeySet('{ENTER}', '_BlockKey')
            EndIf
        Case '{SPACE}'
            If ControlGetFocus(HWnd($Main)) <> 'Button1' Then 
                HotKeySet('{SPACE}')
                Send('{SPACE}')
                HotKeySet('{SPACE}', '_BlockKey')
            EndIf
    EndSwitch
EndFunc
Func _ClickButton()
    ControlClick(HWnd($Main), '', $Button)
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Yeah, retested...

Here's my last attempt, I noticed that I'm the only one trying anything here ;)

Awe... :lmao:

Well CTRL+A and mouse make it go 'tada!' and space and enter push on the dummy button which you cant see =)

#include <GUIConstants.au3>
#include <Misc.au3>

$i = 0
$window = GUICreate("test",200,100)
$button = GUICtrlCreateButton("Button",5,5,190,90)
$dummy = GUICtrlcreatebutton("",200,100,1,1)
HotKeySet("^a","buttonfunc")
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
    If  ControlGetActive() <> $button Then 
        ControlFocus("test","",$dummy)
    EndIf
    If $msg = $button OR $i = 1 Then 
        $i = 0
        SoundPlay(@WindowsDir & "\media\tada.wav")
        ControlFocus("test","",$dummy)
    EndIf
WEnd

Func ControlGetActive()
    Local $temp
    $temp = GUIGetCursorInfo()
    Return $Temp[4]
EndFunc  
Func ButtonFunc()
    $i = 1
EndFunc
Edited by Rad
Link to comment
Share on other sites

Well if you use mine your going to have to change some stuff to make it able to work for multiple buttons, and unless you do a different way your gonna have a func for each button that just sets $i = 1 (didnt want to use _IsPressed, but it would probrably work for you)

Link to comment
Share on other sites

Well if you use mine your going to have to change some stuff to make it able to work for multiple buttons, and unless you do a different way your gonna have a func for each button that just sets $i = 1 (didnt want to use _IsPressed, but it would probrably work for you)

I just have one button.. ALmost the same as you. Program receives tcp packet and sends something back that's all it does. + it has systray menu to resend command etc but that should matter ;)

No worries i'll apply those or SMoke_N example to my code :lmao: just didn't knew where to start on that blocking keys thingy :evil:

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...