Key Stroking With That Technology We Like To Call Shoes

July 31st 01:58
by why

Whether you just like to call it Shoes or you legitimately love to call it Shoes, there’s no question that it really is Shoes. Few people doubt that. And those that do are lifeless goons who would cut a hole through a pelican without batting an eye.

Let’s cover how to capture keystrokes in Shoes. In web browsers, you get back a keycode. On Try Ruby, I spent some time figuring out what codes where what. I sent alot of Germans here.

In Shoes, after a key press, you get something more useful than a keycode. You can expect to get one of these:

  • In the case of a printable letter, a UTF-8 string.
  • Special keys like Backspace come back as a symbol. Such as :backspace.
  • Modified keys come back as a symbol as well. Like :alt_q.

Letters can only be modified right now with the Alt (or Option) key. Special keys can be modified by Control, Shift and Alt. In that order. (So, if you press Control+Shift+Alt+Up, you get :control_shift_alt_up.)


One of the things I’m writing in Shoes is a little text editor. Here’s a snip from the key stroke bit:

str = ""
Shoes.app do
  keypress do |k|
    case k
    when String
      str += k
    when :backspace
      str.slice!(-1)
    when :tab
      str += "  "
    when :alt_q
      quit
    end
  end
end

Source to a working mini-editor based on this code is here.

Now, there are a few things missing from Shoes which could really help this editor out. A cursor. Text selection. Cut, copy and paste. And win32 is missing a vertical scrollbar. I assure these matters are of grave import to me!!

Now begin the comments …

8 comments

RubyPanther

said on July 31st 09:44

It would be nice if modified keys returned both the UTF8 string and also the modifier symbols. Guaranteed to be at least two peaches easier to work with.

Automatthew

said on July 31st 10:23

Dear Why,

You write like the late R. A. Lafferty. This is a good thing. Continue.

Brian

said on July 31st 12:19

I hadn’t read R.A. Lafferty–but will now …

Meanwhile, these Shoes feel just great. Thanks for the creations … The sample mini-editor is the nicest little Ruby-let I have used. It’s just plain fun and cool to run a little bit of code on Windows and Ubuntu and see GUI things point-and-click and work the same way on multiple platforms.

“Try your shoes!” This is software grandmothers will love.

zerohalo

said on July 31st 14:08

Very nice, _why. I think you could be coming up with something that could be easy enough for kids to learn to make their own apps with, using Ruby. I’m going to try it with my 9 yr old daughter and see how she grasps it, as your code progresses.

I’d recommend finding a balance between making things easy but yet not having too much “magic”. The problem with magic is that it makes it easy for the kid to do stuff quickly, but it also provides a crutch to where they’re just doing certain things without understanding the process. If they can actually see the process in the code that they’re writing, even if they have to write a bit more lines of code, I think it helps them to understand the programming flow, which I believe is essential to learning programming--learning how to “think” programming. I’ve been looking at various GUI toolkits for my daughter to progress into creating small games with Ruby, now that we’re getting a bit past the console, but most of them are just a bit complex for a child–though wxSugar and RubyGame are pretty good (RubyGame is of course a completely different animal). I have a feeling you’re hitting on something here with Shoes. Thanks!

zverok

said on July 31st 14:56

I used another approach in my (slightly competing, though still unpublished) library:


widget.on_key_down(’ctrl+a’) do
#only ctrl+a’s would be here
end

widget.on_key_down(’shift+*’) do |event|
#all shift+something would be here
#event.key would be pressed key symbol
end

widget.on_key_down(’*’) do |event|
#all keys WITHOUT modifiers
end

widget.on_key_down do |event|
#all keys
#can examine event.key and event.modifier
end

there is two differences:
1. strings for me seems more natural than symbols (as symbols feels like “something integral”, while strings are “something you can concat and split”, also, strings allow more “natural” shortcuts writing, even “ctrl+shift+=”)
2. independent handlers setting for each shortcut seems more flexible than common “case” solution.

PS: pre closing tag doesn’t work in comments :(

_why

said on July 31st 15:02

zerohalo: Thankyou for your comments. I agree with you. Shoes will have less magic than many of my other libraries. I do have a few shortcuts: like how button { … } is a shortcut for button.click { … }. Anyhow, since it’s all written in C, I can’t really get too tricky with it.

zverok: Those are good points. Similar to RubyPanther’s, in a way. I will probably be reconsidering some of this in light of what you fellows have said.

zerohalo

said on July 31st 16:36

_why, generally I agree with shortcuts especially if they simplify the syntax while still making it clear what is happening. If I may present a contrasting opinion on your button example, just from the standpoint of a child’s understanding (though I understand you’re not writing Shoes for children): I would say that button.click while longer, makes more sense than button. The child sees “button.click { … }” and it’s easy for them to grasp that the block is executed when the button is clicked. Whereas “button { … }” could be interpreted as meaning other things. Likewise if they see button.size() or button.color() it’s apparent that’s the size of the particular button. I can’t say I’ve thought this through really thoroughly so what I suggest may be seriously flawed, so I’m just throwing this out for your consideration, something like:

button(ask) do
self.size = [100,233] # or perhaps self.size(100,233)
self.color = [255,255,255] # or a named color
self.label = “Ask” # you might want to change the button label later
self.position = 2 # I’m not sure about this, but it’s basically a way to change the order of the buttons or widgets in the stack.
self.clicked { do_stuff }
end

PS. I like your keypress method. Makes great sense.

jtoy

said on August 1st 20:14

nice work!!!! this is like a mini adobe air / qt / silverlight / xulrunner engine. This could be an extremely awesome project.

Comments are closed for this entry.