in Education by
I'm trying to make the text move continuously by pressing a button but right now it only moves an interval each time it is pressed. def move(): global y global checkmove checkmove = True if y > 280: y = 0 else: y += 2 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
There is no built-in way to tell if a button is being held down. However, there is a button_up handler as well as a button_down handler. If you use timers, you can tell how long a key has been held down. Here is a sample of code that can tell which keys are held down. Multiple keys can be held down at once. If you want to change the timing, you can change the length of the timers at the bottom of the code. Changing the value of timer 1 will change how long the key has to be held before simulating keypresses, changing the length of timer 2 changes how quickly the keys are pressed once held down. You can also see the code here: https://py3.codeskulptor.org/#user303_gtJ15kIGNV_0.py def timer_handler(): #start timer 2 if it is off if not timer_on: timer2.start() #stop timer 1 timer.stop() def timer2_handler(): #simulates repeated keypresses if the key is held down global timer_on timer_on=True if len(held_keys)==0: timer2.stop() timer_on=False else: for item in held_keys: action(item) def action(key): #this is where all the code to make something happen goes if key==upkey: #do something print('up key pressed') elif key==downkey: #do something else print('down key pressed') def key_handler(key): #handles user keypresses #add key to held keys global held_keys held_keys.append(key) #do something with key action(key) #start timer 1 if timer 2 is off if not timer_on: timer.start() def release_handler(key): #handles key releases #remove the key from the list global held_keys if key in held_keys: held_keys.remove(key) #if no keys are held, stop both timers global timer_on if len(held_keys)==0: timer.stop() timer2.stop() timer_on=False import simplegui timer_on=False held_keys=[] #starts timer 2 timer = simplegui.create_timer(300, timer_handler) #automatic keypress timer2 = simplegui.create_timer(100, timer2_handler) #map whatever keys you need spacekey=simplegui.KEY_MAP['space'] leftkey=simplegui.KEY_MAP['left'] rightkey=simplegui.KEY_MAP['right'] upkey=simplegui.KEY_MAP['up'] downkey=simplegui.KEY_MAP['down'] #create a frame and set key down/up handlers frame=simplegui.create_frame('Click the box, then use arrow keys to move', 100, 100) frame.set_keydown_handler(key_handler) frame.set_keyup_handler(release_handler) frame.start()

Related questions

0 votes
    I have drawn an image in the device context using python and I want to move it smoothly/animate either ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I have drawn an image in the device context using python and I want to move it smoothly/animate either ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I have drawn an image in the device context using python and I want to move it smoothly/animate either ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, ... ] = val return d Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I'm using Python to open a text document: text_file = open("Output.txt", "w") text_file.write("Purchase Amount: ... how to do this? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I am trying to write my below pinging script results into the Text file, but I am getting an error message. ... object is not iterable Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to put time delay in the script of Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how much do Python programmers make? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    As the title suggests, I'm just looking for a way of pressing a button in Shoes without clicking ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
...