WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-25-2008, 09:31 AM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
Thumbs up Keyboard options

Hi,

I am trying to build an application with radio buttons activated by keyboard instead of mouse. I am using these lines

def onKeyDown(key):
if key == 'A':
aRadio.visible(1)
aans = aRadio.get()
print aans

But, say there is another OK button
if key == 'D':
OKbutton.visible(0)
dans = OKbutton.get()
print dans

Now, I am trying, if an user presses the A and D button in succession, then I want some response.

if key == 'A':
aRadio.visible(1)
aans = aRadio.get()
print aans
if key == 'D':
OKbutton.visible(0)
dans = OKbutton.get()
print 'abcd'

But, it is not printing out 'abcd'.

Can anyone suggest where I am going wrong or is there is any better way?

Thanks in advance for the sugestion


Uttama
Reply With Quote
  #2  
Old 06-25-2008, 10:08 AM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
Hi,

I have used a counter and this solves my problem.

But, I don't know, why nested if loop is not sensing.

Thanks

Uttama
Reply With Quote
  #3  
Old 06-25-2008, 11:10 AM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
Hi,

Does nested if loop work in Vizard (particularly while using two buttons of keyboard or a keyboard button and a variable value)?

If somebody has some suggestion....it would be really helpful to me.

Thanks

Uttama
Reply With Quote
  #4  
Old 06-25-2008, 11:29 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Python supports nested loops. Here is a sample:
Code:
for x in range(5):
    for y in range(5):
        print x,y
I'm not sure how this relates to what you are trying to do. There are many ways you can detect consecutive keyboard presses in Vizard, however using nested loops is not related to it.
Reply With Quote
  #5  
Old 06-25-2008, 11:37 AM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
Hello,

I was trying by using nested if loop:

aRadio = QuestionBox.add(viz.RADIO, 0, "A. 1")
bRadio = QuestionBox.add(viz.RADIO, 0, "B. 2")
cRadio = QuestionBox.add(viz.RADIO, 0, "C. 3")
OKbutton = QuestionBox.add(viz.BUTTON, "OK")
def onKeyDown(key):
if key == 'A':
if key == 'D':
print 'abcd'
viz.callback(viz.KEYDOWN_EVENT,onKeyDown)

Here, A is my first option and D is my OK button. So, what I want is if the user presses A and subsequently D (i.e. OK button), I must print something say 'abcd'.

Are there further options available for consecutive keyboard entries?

Thanks

Uttama
Reply With Quote
  #6  
Old 06-25-2008, 11:52 AM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
Hello,

The help file does not show any options for consecutive keyboard entries.

Thanks

Uttama
Reply With Quote
  #7  
Old 06-25-2008, 04:11 PM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
Hi,

No suggestions? If anyone has some suggestion regarding consecutive keyboard entry, please provide.

Thanks

Uttama
Reply With Quote
  #8  
Old 06-26-2008, 11:08 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
I'm not sure what you mean by a 'nested if loop'. The if statement does not loop, it simply checks a condition and executes the nested code if the condition passes. The code you posted would never work because the key variable can never be equal to 'A' and 'D' at the same time.

One way to check for consecutive key presses is to save the last key press and compare it with the new one. Here is some sample code that does this. It also check how long has elapsed since the last press to make sure the keys were pressed in a short amount of time.
Code:
import viz
viz.go()

lastKey = None
lastKeyTime = 0.0
def onKeyDown(key):
	global lastKey,lastKeyTime
	
	elapsed = viz.tick() - lastKeyTime
	
	if lastKey == 'a' and key == 'd' and elapsed < 0.2:
		print 'ad pressed'
	
	lastKey = key
	lastKeyTime = viz.tick()
viz.callback(viz.KEYDOWN_EVENT,onKeyDown)
Reply With Quote
  #9  
Old 06-26-2008, 01:28 PM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
Hello,

Many thanks for the code. It is working perfectly. Your suggestions are really great.

May be, my nested if loop has been interpreted wrongly. What I was trying to say was that nested loop using if statements.

def onKeyDown(key):
if key=='A':
print 'abcd'
if key=='B':
print 'pqrs'
viz.callback(viz.KEYDOWN_EVENT,onKeyDown)

When, I was using the above code and I was pressing 'A' on keyboard, I was getting a printout of 'abcd'. This implies, that the first if statement was checking the conditionality. Thus, once in the first if loop, when I was subsequently pressing the 'B' button on keyboard, I was not getting any output. Does it not imply that it was not taking the second sequential input from keyboard? or may be the 'key' was not taking in the 'B' input.

Thanks

Uttama
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Menu Options south_bank Vizard 5 07-07-2008 10:52 AM
What are the options for making a movie of a Vizard world? Karla Vizard 1 05-13-2008 10:56 AM
On Screen Keyboard betancourtb82 Vizard 14 10-03-2006 12:38 PM
Multiple Viewports in Vizard, Utilizing keyboard callback shivanangel Vizard 2 02-21-2006 04:56 PM
Timer vs. Keyboard input Wenamun Vizard 1 01-23-2006 09:04 PM


All times are GMT -7. The time now is 06:57 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC