Untitled

 avatar
unknown
plain_text
5 months ago
4.0 kB
3
Indexable
class TouchKeyboard(ctk.CTkFrame):
    def __init__(self, parent, entry_widget, dark_mode: bool):
        super().__init__(parent)
        
        self.entry_widget = entry_widget
        self.dark_mode = dark_mode
        self.shift_active = False
        
        # Colors based on dark mode
        self.key_color = '#3a3a3c' if dark_mode else '#d1d1d6'
        self.hover_color = '#2c2c2e' if dark_mode else '#e5e5e7'
        self.text_color = 'white' if dark_mode else 'black'
        
        self.create_keyboard()
        
    def create_keyboard(self):
        # Define keyboard layouts
        layouts = {
            'normal': [
                ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
                ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
                ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
                ['⇧', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '⌫'],
                ['123', 'space', '.', 'Enter']
            ],
            'shift': [
                ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
                ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'],
                ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'],
                ['⇧', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '⌫'],
                ['123', 'space', '.', 'Enter']
            ],
            'symbols': [
                ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
                ['@', '#', '$', '%', '&', '*', '-', '+', '(', ')'],
                ['!', '"', '\'', ':', ';', '/', '?', '\\', '='],
                ['ABC', '<', '>', '{', '}', '[', ']', '_', '⌫'],
                ['123', 'space', '.', 'Enter']
            ]
        }
        
        self.current_layout = 'normal'
        self.layouts = layouts
        
        # Create keyboard rows
        for row_keys in self.layouts[self.current_layout]:
            row = ctk.CTkFrame(self, fg_color='transparent')
            row.pack(pady=2)
            
            for key in row_keys:
                if key == 'space':
                    width = 200  # Wider space bar
                else:
                    width = 45
                    
                btn = ctk.CTkButton(
                    row,
                    text=key,
                    width=width,
                    height=45,
                    fg_color=self.key_color,
                    hover_color=self.hover_color,
                    text_color=self.text_color,
                    font=("Dongle", 20),
                    command=lambda k=key: self.key_press(k)
                )
                btn.pack(side='left', padx=2)
                
    def key_press(self, key):
        if key == '⇧':
            self.shift_active = not self.shift_active
            self.current_layout = 'shift' if self.shift_active else 'normal'
            self.update_layout()
        elif key == '123' or key == 'ABC':
            self.current_layout = 'symbols' if key == '123' else 'normal'
            self.update_layout()
        elif key == '⌫':
            # Delete last character
            current = self.entry_widget.get()
            self.entry_widget.delete(0, 'end')
            self.entry_widget.insert(0, current[:-1])
        elif key == 'space':
            self.entry_widget.insert('end', ' ')
        elif key == 'Enter':
            # You can add specific Enter key behavior here
            pass
        else:
            # Insert the character
            self.entry_widget.insert('end', key)
            if self.shift_active:
                self.shift_active = False
                self.current_layout = 'normal'
                self.update_layout()
                
    def update_layout(self):
        # Remove all existing keys
        for widget in self.winfo_children():
            widget.destroy()
            
        # Recreate with new layout
        self.create_keyboard()
Editor is loading...
Leave a Comment