def build_key_steps(
    count, new_keys, base_keys, 
    words, bad_words):

    CONGRATS = [
        _('Well done!'),
        _('Good job.'),
        _('Awesome!'),
        _('Way to go!'),
        _('Wonderful!'),
        _('Nice work.'),
        _('You did it!'),
    ]

    def get_congrats():
        return random.choice(CONGRATS) + ' '

    HINTS = [
        _('Be careful to use the correct finger to press each key.  Look at the keyboard below if you need help remembering.'),
        _('Try to type at the same speed, all the time.  As you get more comfortable you can increase the speed a little.')
    ]

    def get_hint():
        return random.choice(HINTS)

    FINGERS = {
        'LP': _('left little'),
        'LR': _('left ring'),
        'LM': _('left middle'),
        'LI': _('left index'),
        'LT': _('left thumb'),
        'RP': _('right little'),
        'RR': _('right ring'),
        'RM': _('right middle'),
        'RI': _('right index'),
        'RT': _('right thumb'),
    }

    all_keys = new_keys + base_keys

    good_words = filter_wordlist(words=words, 
        all_keys=all_keys, req_keys=new_keys, 
        minlen=2, maxlen=8, 
        bad_words=bad_words)
    
    pairs = get_pairs_from_wordlist(good_words)
        
    steps = []

    kb = keyboard.KeyboardData()

    # Attempt to load a letter map for the current locale.
    code = locale.getdefaultlocale()[0] or 'en_US'
    try:
        kb.load_letter_map('lessons/%s.key' % code)
    except:
        kb.load_letter_map('lessons/en_US.key')

    kb.set_layout(keyboard.get_layout())

    keynames = new_keys[0]
    if len(new_keys) >= 2:
        for k in new_keys[1:-1]:
            keynames += ', ' + k
        keynames += _(' and %s keys') % new_keys[-1]
    else:
        keynames += _(' key')

    steps.append(make_step(
        _('In this lesson, you will learn the %(keynames)s.\n\nPress the ENTER key when you are ready to begin!') \
            % { 'keynames': keynames },
        'key', '\n'))

#    for letter in new_keys:
#        key, state, group = kb.get_key_state_group_for_letter(letter)
#
#        if not key:
#            error("There is no key combination in the current keymap for the '%s' letter. " % letter + \
#                  "Are you sure the keymap is set correctly?\n")
#
#        try:
#            finger = FINGERS[key['key-finger']]
#        except:
#            error("The '%s' letter (scan code %x) does not have a finger assigned." % (letter, key['key-scan']))
#
#        if state == Gdk.ModifierType.SHIFT_MASK:
#            # Choose the finger to press the SHIFT key with.
#            if key['key-finger'][0] == 'R':
#                shift_finger = FINGERS['LP']
#            else:
#                shift_finger = FINGERS['RP']
#
#            instructions = _('Press and hold the SHIFT key with your %(finger)s finger, ') % { 'finger': shift_finger }
#            instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger }
#
#        elif state == Gdk.ModifierType.MOD5_MASK:
#            instructions = _('Press and hold the ALTGR key, ') 
#            instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger }
#
#        elif state == Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.MOD5_MASK:
#            instructions = _('Press and hold the ALTGR and SHIFT keys, ')
#            instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger }
#
#        else:
#            instructions = _('Press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger }
#
#        steps.append(make_step(instructions, 'key', letter))

    steps.append(make_step(_("Let\'s learn the new keys."), 'key', ''.join(new_keys) * 4))

    steps.append(make_step(
        get_congrats() + _('Practice typing the keys you just learned.'),
        'text', make_random_doubles(new_keys, count)))
    
    steps.append(make_step(
        get_congrats() + _('Now put the keys together into pairs.'),
        'text', make_weighted_wordlist_pairs(pairs, new_keys, all_keys, count)))
    
    if len(good_words) == 0:
        steps.append(make_step(
            get_congrats() + _('Time to type jumbles.'),
            'text', make_jumbles(new_keys, all_keys, count, 5)))
        
    else:
        steps.append(make_step(
            get_congrats() + _('Time to type real words.'),
            'text', make_random_words(good_words, new_keys, all_keys, count)))
    
    return steps 
    def __init__(self, lesson, keyboard_images, activity):
        GObject.GObject.__init__(self)
        
        self.lesson = lesson
        self.keyboard_images = keyboard_images
        self.activity = activity
        
        # Build the user interface.
        title = Gtk.Label()
        title.set_markup("<span size='x-large' weight='bold'>" + lesson['name'] + "</span>")
        title.set_alignment(1.0, 0.0)
        
        stoplabel = Gtk.Label(label=_('Go Back'))
        stopbtn =  Gtk.Button()
        stopbtn.add(stoplabel)
        stopbtn.connect('clicked', self.stop_cb)
        
        # TODO- These will be replaced by graphical displays using Gtk.DrawingArea.
        self.wpm = 0
        self.accuracy = 0
        
        self.wpmlabel = Gtk.Label()
        self.accuracylabel = Gtk.Label()
        
        #self.wpmarea = Gtk.DrawingArea()
        #self.wpmarea.connect('expose-event', self.wpm_expose_cb)
        #self.accuracyarea = Gtk.DrawingArea()
        #self.accuracyarea.connect('expose-event', self.accuracy_expose_cb)
        
        hbox = Gtk.HBox()
        hbox.pack_start(stopbtn, False, False, 10)
        hbox.pack_start(self.wpmlabel, True, False, 10)
        hbox.pack_start(self.accuracylabel, True, False, 10)
        hbox.pack_end(title, False, False, 10)
        
        # Set up font styles.
        self.tagtable = Gtk.TextTagTable()
        instructions_tag = Gtk.TextTag.new('instructions')
        instructions_tag.props.justification = Gtk.Justification.CENTER
        self.tagtable.add(instructions_tag)

        text_tag = Gtk.TextTag.new('text')
        text_tag.props.family = 'Monospace'
        self.tagtable.add(text_tag)
        
        spacer_tag = Gtk.TextTag.new('spacer')
        spacer_tag.props.size = 3000
        self.tagtable.add(spacer_tag)
        
        image_tag = Gtk.TextTag.new('image')
        image_tag.props.justification = Gtk.Justification.CENTER
        self.tagtable.add(image_tag)
        
        correct_copy_tag = Gtk.TextTag.new('correct-copy')
        correct_copy_tag.props.family = 'Monospace'
        correct_copy_tag.props.foreground = '#0000ff'
        self.tagtable.add(correct_copy_tag)
        
        incorrect_copy_tag = Gtk.TextTag.new('incorrect-copy')
        incorrect_copy_tag.props.family = 'Monospace'
        incorrect_copy_tag.props.foreground = '#ff0000'
        self.tagtable.add(incorrect_copy_tag)
        
        # Set up the scrolling lesson text view.
        self.lessonbuffer = Gtk.TextBuffer.new(self.tagtable)
        self.lessontext = Gtk.TextView.new_with_buffer(self.lessonbuffer)
        self.lessontext.set_editable(False)
        self.lessontext.set_left_margin(20)
        self.lessontext.set_right_margin(20)
        self.lessontext.set_wrap_mode(Gtk.WrapMode.WORD)
        self.lessontext.modify_base(Gtk.StateType.NORMAL, Gdk.Color.parse('#ffffcc')[1])
        
        self.lessonscroll = Gtk.ScrolledWindow()
        self.lessonscroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.ALWAYS)
        self.lessonscroll.add(self.lessontext)
        
        frame = Gtk.Frame()
        frame.add(self.lessonscroll)
        
        self.keyboard = keyboard.KeyboardWidget(self.keyboard_images, self.activity)
        
        # Attempt to load a letter map for the current locale.
        code = locale.getdefaultlocale()[0] or 'en_US'
        try:
            self.keyboard.load_letter_map('lessons/%s.key' % code)
        except:
            pass

        self.keyboard.set_layout(keyboard.get_layout())

        self.pack_start(hbox, False, False, 10)
        self.pack_start(frame, True, True, 0)
        self.pack_start(self.keyboard, False, True, 0)
        
        # Connect keyboard grabbing and releasing callbacks.        
        self.connect('realize', self.realize_cb)
        self.connect('unrealize', self.unrealize_cb)
        
        self.show_all()

        self.timer_id = None
        
        self.begin_lesson()
    def __init__(self, lesson, keyboard_images, activity):
        GObject.GObject.__init__(self)

        self.lesson = lesson
        self.keyboard_images = keyboard_images
        self.activity = activity

        # Build the user interface.
        title = Gtk.Label()
        title.set_markup("<span size='x-large' weight='bold'>" +
                         lesson['name'] + "</span>")
        title.set_alignment(1.0, 0.0)

        stoplabel = Gtk.Label(label=_('Go Back'))
        stopbtn = Gtk.Button()
        stopbtn.add(stoplabel)
        stopbtn.connect('clicked', self.stop_cb)

        # TODO- These will be replaced by graphical displays using Gtk.DrawingArea.
        self.wpm = 0
        self.accuracy = 0

        self.wpmlabel = Gtk.Label()
        self.accuracylabel = Gtk.Label()

        #self.wpmarea = Gtk.DrawingArea()
        #self.wpmarea.connect('expose-event', self.wpm_expose_cb)
        #self.accuracyarea = Gtk.DrawingArea()
        #self.accuracyarea.connect('expose-event', self.accuracy_expose_cb)

        hbox = Gtk.HBox()
        hbox.pack_start(stopbtn, False, False, 10)
        hbox.pack_start(self.wpmlabel, True, False, 10)
        hbox.pack_start(self.accuracylabel, True, False, 10)
        hbox.pack_end(title, False, False, 10)

        # Set up font styles.
        self.tagtable = Gtk.TextTagTable()
        instructions_tag = Gtk.TextTag.new('instructions')
        instructions_tag.props.justification = Gtk.Justification.CENTER
        self.tagtable.add(instructions_tag)

        text_tag = Gtk.TextTag.new('text')
        text_tag.props.family = 'Monospace'
        self.tagtable.add(text_tag)

        spacer_tag = Gtk.TextTag.new('spacer')
        spacer_tag.props.size = 3000
        self.tagtable.add(spacer_tag)

        image_tag = Gtk.TextTag.new('image')
        image_tag.props.justification = Gtk.Justification.CENTER
        self.tagtable.add(image_tag)

        correct_copy_tag = Gtk.TextTag.new('correct-copy')
        correct_copy_tag.props.family = 'Monospace'
        correct_copy_tag.props.foreground = '#0000ff'
        self.tagtable.add(correct_copy_tag)

        incorrect_copy_tag = Gtk.TextTag.new('incorrect-copy')
        incorrect_copy_tag.props.family = 'Monospace'
        incorrect_copy_tag.props.foreground = '#ff0000'
        self.tagtable.add(incorrect_copy_tag)

        # Set up the scrolling lesson text view.
        self.lessonbuffer = Gtk.TextBuffer.new(self.tagtable)
        self.lessontext = Gtk.TextView.new_with_buffer(self.lessonbuffer)
        self.lessontext.set_editable(False)
        self.lessontext.set_left_margin(20)
        self.lessontext.set_right_margin(20)
        self.lessontext.set_wrap_mode(Gtk.WrapMode.WORD)
        self.lessontext.modify_base(Gtk.StateType.NORMAL,
                                    Gdk.Color.parse('#ffffcc')[1])

        self.lessonscroll = Gtk.ScrolledWindow()
        self.lessonscroll.set_policy(Gtk.PolicyType.AUTOMATIC,
                                     Gtk.PolicyType.ALWAYS)
        self.lessonscroll.add(self.lessontext)

        frame = Gtk.Frame()
        frame.add(self.lessonscroll)

        self.keyboard = keyboard.KeyboardWidget(self.keyboard_images,
                                                self.activity)

        # Attempt to load a letter map for the current locale.
        code = locale.getdefaultlocale()[0] or 'en_US'
        try:
            self.keyboard.load_letter_map('lessons/%s.key' % code)
        except:
            pass

        self.keyboard.set_layout(keyboard.get_layout())

        self.pack_start(hbox, False, False, 10)
        self.pack_start(frame, True, True, 0)
        self.pack_start(self.keyboard, False, True, 0)

        # Connect keyboard grabbing and releasing callbacks.
        self.connect('realize', self.realize_cb)
        self.connect('unrealize', self.unrealize_cb)

        self.show_all()

        self.timer_id = None

        self.begin_lesson()
Exemplo n.º 4
0
# window = Gtk.Window(Gtk.WindowType.TOPLEVEL) Removed above line to because of GtkDeprecation of positional arguments
window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
window.set_title("keyboard widget")
window.connect("destroy", lambda w: Gtk.main_quit())
window.show_all()
window.realize()

image = keyboard.KeyboardImages(800, 400)

k = keyboard.KeyboardWidget(image, window, poll_keys=True)
try:
    k.load_letter_map(sys.argv[1])
except:
    pass
k.set_layout(keyboard.get_layout())

savebtn = Gtk.Button()
savebtn.add(Gtk.Label(label='Save Keys'))
savebtn.connect('clicked', lambda w: k.save_letter_map(sys.argv[1]))

quitbtn = Gtk.Button()
quitbtn.add(Gtk.Label(label='Quit'))
quitbtn.connect('clicked', lambda w: Gtk.main_quit())

hbox = Gtk.HBox()
hbox.pack_start(savebtn, True, True, 0)
hbox.pack_start(quitbtn, True, True, 0)

vbox = Gtk.VBox()
vbox.pack_start(k, True, True, 0)
Exemplo n.º 5
0
from gi.repository import Gtk

window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
window.set_title("keyboard widget")
window.connect("destroy", lambda w: Gtk.main_quit())
window.show_all()
window.realize()

image = keyboard.KeyboardImages(800,400)

k = keyboard.KeyboardWidget(image, window, poll_keys=True)
try:
    k.load_letter_map(sys.argv[1])
except:
    pass
k.set_layout(keyboard.get_layout())

savebtn = Gtk.Button()
savebtn.add(Gtk.Label(label='Save Keys'))
savebtn.connect('clicked', lambda w: k.save_letter_map(sys.argv[1]))

quitbtn = Gtk.Button()
quitbtn.add(Gtk.Label(label='Quit'))
quitbtn.connect('clicked', lambda w: Gtk.main_quit())

hbox = Gtk.HBox()
hbox.pack_start(savebtn, True, True, 0)
hbox.pack_start(quitbtn, True, True, 0)

vbox = Gtk.VBox()
vbox.pack_start(k, True, True, 0)
def build_key_steps(
    count, new_keys, base_keys, 
    words, bad_words):

    CONGRATS = [
        _('Well done!'),
        _('Good job.'),
        _('Awesome!'),
        _('Way to go!'),
        _('Wonderful!'),
        _('Nice work.'),
        _('You did it!'),
    ]

    def get_congrats():
        return random.choice(CONGRATS) + ' '

    HINTS = [
        _('Be careful to use the correct finger to press each key.  Look at the keyboard below if you need help remembering.'),
        _('Try to type at the same speed, all the time.  As you get more comfortable you can increase the speed a little.')
    ]

    def get_hint():
        return random.choice(HINTS)

    FINGERS = {
        'LP': _('left little'),
        'LR': _('left ring'),
        'LM': _('left middle'),
        'LI': _('left index'),
        'LT': _('left thumb'),
        'RP': _('right little'),
        'RR': _('right ring'),
        'RM': _('right middle'),
        'RI': _('right index'),
        'RT': _('right thumb'),
    }

    all_keys = new_keys + base_keys

    good_words = filter_wordlist(words=words, 
        all_keys=all_keys, req_keys=new_keys, 
        minlen=2, maxlen=8, 
        bad_words=bad_words)
    
    pairs = get_pairs_from_wordlist(good_words)
        
    steps = []

    kb = keyboard.KeyboardData()

    # Attempt to load a letter map for the current locale.
    code = locale.getdefaultlocale()[0] or 'en_US'
    try:
        kb.load_letter_map('lessons/%s.key' % code)
    except:
        kb.load_letter_map('lessons/en_US.key')

    kb.set_layout(keyboard.get_layout())

    keynames = new_keys[0]
    if len(new_keys) >= 2:
        for k in new_keys[1:-1]:
            keynames += ', ' + k
        keynames += _(' and %s keys') % new_keys[-1]
    else:
        keynames += _(' key')

    steps.append(make_step(
        _('In this lesson, you will learn the %(keynames)s.\n\nPress the ENTER key when you are ready to begin!') \
            % { 'keynames': keynames },
        'key', '\n'))

#    for letter in new_keys:
#        key, state, group = kb.get_key_state_group_for_letter(letter)
#
#        if not key:
#            error("There is no key combination in the current keymap for the '%s' letter. " % letter + \
#                  "Are you sure the keymap is set correctly?\n")
#
#        try:
#            finger = FINGERS[key['key-finger']]
#        except:
#            error("The '%s' letter (scan code %x) does not have a finger assigned." % (letter, key['key-scan']))
#
#        if state == Gdk.ModifierType.SHIFT_MASK:
#            # Choose the finger to press the SHIFT key with.
#            if key['key-finger'][0] == 'R':
#                shift_finger = FINGERS['LP']
#            else:
#                shift_finger = FINGERS['RP']
#
#            instructions = _('Press and hold the SHIFT key with your %(finger)s finger, ') % { 'finger': shift_finger }
#            instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger }
#
#        elif state == Gdk.ModifierType.MOD5_MASK:
#            instructions = _('Press and hold the ALTGR key, ') 
#            instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger }
#
#        elif state == Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.MOD5_MASK:
#            instructions = _('Press and hold the ALTGR and SHIFT keys, ')
#            instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger }
#
#        else:
#            instructions = _('Press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger }
#
#        steps.append(make_step(instructions, 'key', letter))

    steps.append(make_step(_("Let\'s learn the new keys."), 'key', ''.join(new_keys) * 4))

    steps.append(make_step(
        get_congrats() + _('Practice typing the keys you just learned.'),
        'text', make_random_doubles(new_keys, count)))
    
    steps.append(make_step(
        get_congrats() + _('Now put the keys together into pairs.'),
        'text', make_weighted_wordlist_pairs(pairs, new_keys, all_keys, count)))
    
    if len(good_words) == 0:
        steps.append(make_step(
            get_congrats() + _('Time to type jumbles.'),
            'text', make_jumbles(new_keys, all_keys, count, 5)))
        
    else:
        steps.append(make_step(
            get_congrats() + _('Time to type real words.'),
            'text', make_random_words(good_words, new_keys, all_keys, count)))
    
    return steps