Home > Mobile >  What keysym does the <Button-1> (left mouse button )have?
What keysym does the <Button-1> (left mouse button )have?

Time:03-07

I have a function:

def sound(self,event):
        playsound('monkey_sound.wav', block=False)

It responds to pressing only one key - the left mouse button. I have to make it in that way, that sound will be different depending on the pressed key (left mouse button or right mouse button). But there should be only one function.

Here is fragment of my code:

class monkey:
    def __init__(self, canvas):
        self.canvas=canvas
        self.photo = PhotoImage(file='C:\\monkey_exe\\monkey.png')
        self.id=canvas.create_image(30,30,anchor=NW,image=self.photo)
        self.canvas.bind_all('<Motion>', self.motion)
        self.canvas.bind_all('<Button-1>', self.sound)
        self.canvas.bind_all('<Button-2>', self.sound)
    def motion(self,event):
        canvas.coords(self.id, event.x-50, event.y-108)
    def sound(self,event):
        playsound('monkey_sound.wav', block=False)

So the two keys must be binded to the same function, but they must play different sounds. But I don't know keysym's of left mouse button and right mouse button.

CodePudding user response:

I am unsure on what the symbol of the mouse buttons, but I found a decent alternative with using the event.num property...

def sound(self, event):
    if event.num == 1: #left mouse button
        playsound("monkey_sound1.wav", block=false)

    elif event.num == 2: #middle mouse button
        playsound("monkey_sound2.wav", block=false)

    elif event.num == 3: #right mouse button
        playsound("monkey_sound3.wav", block=false)

The event.num will return a number between 1-3 (unless you bind the function to another key/button).

  • Related