[racket] key event problem
On Wed, Apr 18, 2012 at 12:39, Roelof Wobben <r.wobben at home.nl> wrote:
> Op 18-4-2012 12:32, Pierpaolo Bernardi schreef:
>
>> On Wed, Apr 18, 2012 at 11:17, Roelof Wobben<r.wobben at home.nl> wrote:
>>>
>>> Hello,
>>>
>>> I try to make a function which controls the key-events.
>>>
>>> The function does only have to react on backspace , left , right .
>>> Give no reaction on tab , rubout or the other keys with a length more
>>> then
>>> 1.
>>> When given another key with length 1 it has to insert the key.
>>>
>>> So i thought this would work :
>>>
>>> (define (edit e k)
>>> (cond
>>> [ (and(key=? "right" k) (> (string-length (editor-post e)) 0))
>>> (make-editor(string-append (editor-pre e) (string-first (editor-post e)
>>> ))
>>> (string-rest (editor-post e) ))]
>>> [ (and(key=? "left" k) (> (string-length (editor-pre e)) 0))
>>> (make-editor ( string-remove-last (editor-pre e) ) (string-append
>>> (string-last(editor-pre e)) (editor-post e)))]
>>> [ (and (key=? "\b" k) (> (string-length (editor-pre e)) 0))
>>> (make-editor (string-remove-last (editor-pre e)) (editor-post e))]
>>> [ (not ( or (key=? "\t" k ) (key=? "\u007F" k) (key=? "right" k))) (
>>> string-append (editor-pre e) k)]
>>> [else e]
>>> ))
>>>
>>> but now when someone press the right-key the first get executed but also
>>> the
>>> not rule.
>>> I thought that cond works if one rule is true the other gets ignored. But
>>> it
>>> looks like that's not true.
>>>
>>> Now my question is : how can I take care that all the other key's with
>>> length 1 except backspace, tab, and rubout get's executed ?
>>
>> maybe:
>>
>> (define (edit e k)
>> (cond ((key=? "right" k)
>> (if (zero? (string-length (editor-post e)))
>> e
>> <create an updated new editor>))
>> ((key=? "left" k)
>> (if (zero? (string-length (editor-pre e)))
>> e
>> <create an updated new editor>))
>> ...
>> ))
>>
>> ?
>>
>
> oke, but why string-length 0 . In my opinion the string-length of the key is
> 1 or more then 1.
>
> So I thought of this :
>
>
> (define (edit e k)
> (cond
> [ (and(key=? "right" k) (> (string-length (editor-post e)) 0))
> (make-editor(string-append (editor-pre e) (string-first (editor-post e) ))
> (string-rest (editor-post e) ))]
> [ (and(key=? "left" k) (> (string-length (editor-pre e)) 0))
> (make-editor ( string-remove-last (editor-pre e) ) (string-append
> (string-last(editor-pre e)) (editor-post e)))]
> [ (and (>(string-length k)1) (not (or(key=? "left" k) (key=? "right" k)))
> e
>
> [ (and (key=? "\b" k) (> (string-length (editor-pre e)) 0))
> (make-editor (string-remove-last (editor-pre e)) (editor-post e))]
> [ (and (equal (string-length k)1) (not ( or (key=? "\t" k ) (key=?
> "\u007F" k) (key=? "bs" k))) (
>
> string-append (editor-pre e) k)]
> [else e]
> ))
>
> This is not tested and can have bugs.
Please format your code better. As it is is unreadable.
After reformatting, removing a redundant cond clause, and fixing a
small bug, your code looks like
(define (edit e k)
(cond ((and (key=? "right" k)
(> (string-length (editor-post e)) 0))
(make-editor (string-append (editor-pre e)
(string-first (editor-post e)))
(string-rest (editor-post e))))
((and (key=? "left" k)
(> (string-length (editor-pre e)) 0))
(make-editor (string-remove-last (editor-pre e))
(string-append (string-last(editor-pre e))
(editor-post e))))
((and (key=? "\b" k)
(> (string-length (editor-pre e)) 0))
(make-editor (string-remove-last (editor-pre e))
(editor-post e)))
((= (string-length k) 1)
(make-editor (string-append (editor-pre e) k)
(editor-post e)))
(else e)))
Which, without testing, seems to me that it should work.