KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

Checking if key is pressed when clicking the mousebutton?

 
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion
View previous topic :: View next topic  
Author Message
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Thu Dec 06, 2007 7:05 am    Post subject: Checking if key is pressed when clicking the mousebutton? Reply with quote

Hi!

Is there a way to check if a certain key is pressed when the mouse is clicked?

I have created a clickable label, and I would like it to behave in one way "normally", but if say the Control-key is pressed when clicking the label, it will do something else.

I have a small sample here which does the "normal" part, but after that, I have no clue.

/Viggen


Code:
Break On
$System = CreateObject("Kixforms.System")
If Not $System
   $nul= MessageBox("KiXforms.Net Not Initiated. This Script Will Now Close.","Error",16)
   Quit()
EndIf
$nul = $System.Application.EnableVisualStyles

$Form = $System.Form()
$Form.Left = 0
$Form.StartPosition = 0  ;FormStartPosition_Manual
$Form.Size = $System.Size(365,214) ;(Width,Height)
$Form.Text = "Form2"
$Form.Top = 0

$Label1 = $System.Label()
$Label1.Font = $System.Font("Microsoft Sans Serif",8.25,4) ;Underline
$Label1.ForeColor = $System.Color.FromName("HotTrack")
$Label1.Height = 12
$Label1.Left = 92
$Label1.Text = "www.google.com"
$Label1.TextAlign = 32  ;MiddleCenter
$Label1.Top = 65
$Label1.Width = 150
$Label1.Click = 'test("http://www.google.com")'
$Label1.Cursor = $System.Cursors.Hand
$nul = $Form.Controls.Add($Label1)

$Form.Show  ;Displays the Form

While $Form.Visible
   $Nul = Execute($Form.DoEvents())
Loop
Exit 0


function test($URL)
  Run 'explorer.exe "' + $URL + '"'
endfunction
Back to top
View user's profile Send private message MSN Messenger
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Thu Dec 06, 2007 2:04 pm    Post subject: Reply with quote

Hi Viggen,

I have added a couple lines near the top:
Code:

Global $CtrlKeyDown
$CtrlKeyDown = 0 ;no

these lines define '$CtrlKeyDown' as a Global var (so that it can be seen anywhere within the script) and sets a value of zero to it so by default the ctrl key is not pressed (this is actually only used as a reference var, does not contain any key).

I have added a line in the $Form definition:
Code:

$Form.KeyDown = "CheckForKeyDown()"

this line is the actual trigger for the 'KeyDown' event, if a key is pressed it executes the 'CheckForKeyDown()' function, and it alters the value of '$CtrlKeyDown' = pressed or 1.
Code:

Function CheckForKeyDown()
   $KeyChar = $Form.KeyDownEventArgs.KeyCode
   If $KeyChar = 17
      $CtrlKeyDown = 1 ;yes
  EndIf
EndFunction


I added a seconed line in the $Form definition:
Code:

$Form.KeyUp = "CheckForKeyUp()"

this line is the actual trigger for the 'KeyUp' event, if a key is released it executes the 'CheckForKeyUp()' function, and it alters the value of '$CtrlKeyDown' = released or 0.
Code:

Function CheckForKeyUp()
   $KeyChar = $Form.KeyDownEventArgs.KeyCode
   If $KeyChar = 17
    $CtrlKeyDown = 0 ;no
  EndIf
EndFunction


I have altered your 'test()' Function to include an If statement to check if the val of '$CtrlKeyDown' is pressed or not (1/0):
Code:

Function test($URL)
  If $CtrlKeyDown = 1 ;yes
    ? 'do something special'
  Else
    Run 'explorer.exe "' + $URL + '"'
  EndIf
EndFunction

Hope this is what you are looking for:
Code:

Break On
$System = CreateObject("Kixforms.System")
If Not $System
   $nul= MessageBox("KiXforms.Net Not Initiated. This Script Will Now Close.","Error",16)
   Quit()
EndIf
$nul = $System.Application.EnableVisualStyles

Global $CtrlKeyDown
$CtrlKeyDown = 0 ;no

$Form = $System.Form()
$Form.KeyDown = "CheckForKeyDown()"
$Form.KeyUp = "CheckForKeyUp()"
$Form.Left = 0
$Form.StartPosition = 0  ;FormStartPosition_Manual
$Form.Size = $System.Size(365,214) ;(Width,Height)
$Form.Text = "Form2"
$Form.Top = 0

$Label1 = $System.Label()
$Label1.Font = $System.Font("Microsoft Sans Serif",8.25,4) ;Underline
$Label1.ForeColor = $System.Color.FromName("HotTrack")
$Label1.Height = 12
$Label1.Left = 92
$Label1.Text = "www.google.com"
$Label1.TextAlign = 32  ;MiddleCenter
$Label1.Top = 65
$Label1.Width = 150
$Label1.Click = 'test("http://www.google.com")'
$Label1.Cursor = $System.Cursors.Hand
$nul = $Form.Controls.Add($Label1)

$Form.Show  ;Displays the Form

While $Form.Visible
   $Nul = Execute($Form.DoEvents())
Loop
Exit 0

Function test($URL)
  If $CtrlKeyDown = 1 ;yes
    ? 'do something special'
  Else
    Run 'explorer.exe "' + $URL + '"'
  EndIf
EndFunction

Function CheckForKeyDown()
   $KeyChar = $Form.KeyDownEventArgs.KeyCode
   If $KeyChar = 17
      $CtrlKeyDown = 1 ;yes
  EndIf
EndFunction

Function CheckForKeyUp()
   $KeyChar = $Form.KeyDownEventArgs.KeyCode
   If $KeyChar = 17
    $CtrlKeyDown = 0 ;no
  EndIf
EndFunction

_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Fri Dec 07, 2007 9:22 am    Post subject: Reply with quote

Hi Benny!

As allways your answer i spot on. Smile

Thanks!


/Viggen
Back to top
View user's profile Send private message MSN Messenger
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Fri Dec 07, 2007 12:38 pm    Post subject: Reply with quote

glad I could help Wink
_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group