sketchucation logo sketchucation
    • 登入
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🔌 Smart Spline | Fluid way to handle splines for furniture design and complex structures. Download

    OnKeyDown: getting the charcater value from key

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    32 貼文 7 Posters 2.2k 瀏覽 7 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • tt_suT 離線
      tt_su
      最後由 編輯

      @anton_s said:

      Nah, both of them are encoded in US-ASCII. 😕

      You're talking about ToAscii?
      I was looking at ToUnicode.

      @anton_s said:

      adding char.force_encoding('UTF-8') should do the tick, aint it?

      I'd be careful with force_encoding - it should only be used if you have raw binary data representing a string or if you know the string is incorrectly encoded. Otherwise one should transpose the en encoding.
      foo = bar.encode("UTF-8")

      1 條回覆 最後回覆 回覆 引用 0
      • A 離線
        Anton_S
        最後由 編輯

        @tt_su said:

        You're talking about ToAscii?
        I was looking at ToUnicode.

        Yes, the ToUnicode and ToAscii both return same formatted Ruby strings, but on the actual approach they don't. When I initially created a buffer char = 0.chr*2 it was already formatted in US-ASCII. The ToUnicodeand ToAscii simply fills the the specified buffer with ascii/unicode characters without changing the format of the Ruby string.

        Changing char = 0.chr*2 to char= ' ' will initially create a UTF-8 formatted string, rather than the US-ASCII. The format remains unchanged when the ToUnicode/ToAscii functions fills the char string.

        @tt_su said:

        I'd be careful with force_encoding...

        For some reason force_encoding even managed to crash SketchUp at some points when testing key_test.

        1 條回覆 最後回覆 回覆 引用 0
        • Dan RathbunD 離線
          Dan Rathbun
          最後由 編輯

          Ya'all need to be careful where testing string encoding, until MR1 comes out.
          The correct default encoding is not properly set in MR0.

          I'm not here much anymore.

          1 條回覆 最後回覆 回覆 引用 0
          • D 離線
            driven
            最後由 編輯

            @dan rathbun said:

            The correct default encoding is not properly set in MR0.

            if anyones prepared to admit it...

            the other issue is why these things are so difficult on a mac...

            apple doesn't support key logging and goes out of it's way to prevent it occurring...

            apple does allow it IF it's for assistive devices to aid those with disabilities...

            if you want to use 'assistive devices api' to do ordinary tasks, you need to connivence the 'admin' user to turn it on.

            mac folk are reluctant to do this as it's system wide, and would enable malicious 'key loggers' to, key-log...

            john

            learn from the mistakes of others, you may not live long enough to make them all yourself...

            1 條回覆 最後回覆 回覆 引用 0
            • Dan RathbunD 離線
              Dan Rathbun
              最後由 編輯

              Err.. found a Ruby Core bug.
              Even after setting Encoding::default_internal="UTF-8"
              the Integer#chr() method does not return a string in the default internal encoding.

              .. however there is a workaround. Ruby 2.0's Integer#chr() method now takes an encoding argument, so, this:
              char = 0.chr(Encoding::default_internal)*2
              or this:
              char = 0.chr("UTF-8")*2
              WILL work.

              I'm not here much anymore.

              1 條回覆 最後回覆 回覆 引用 0
              • A 離線
                Anton_S
                最後由 編輯

                @dan rathbun said:

                char = 0.chr("UTF-8")*2

                Nice find Dan 👍 I re-edited the Ruby code above.

                1 條回覆 最後回覆 回覆 引用 0
                • Dan RathbunD 離線
                  Dan Rathbun
                  最後由 編輯

                  @anton_s said:

                  @dan rathbun said:

                  char = 0.chr("UTF-8")*2

                  Nice find Dan 👍 I re-edited the Ruby code above.

                  But you did not enclosed the encoding nickname string in quotes. That'll raise a NameError exception ("uninitialized constant UTF".)

                  I'm not here much anymore.

                  1 條回覆 最後回覆 回覆 引用 0
                  • A 離線
                    Anton_S
                    最後由 編輯

                    Okay, I figured a way to get key via the Windows API. The code below will give you the key code of the exact character that your pressing. For instance,

                    • Pressing 1 yields 1; Pressing Shift-1 returns !.
                    • Pressing w yields w; Pressing Shift-w returns W.

                    I guess it should work properly on the Français keyboard as well. Test it out.

                    require 'Win32API'
                    
                    class KeyTest
                    
                      GetKeyboardState = Win32API.new('User32', 'GetKeyboardState', 'P', 'L')
                      ToAscii          = Win32API.new('User32', 'ToAscii', 'LLPPL', 'L')
                      ToUnicode        = Win32API.new('User32', 'ToUnicode', 'LLPPLL', 'L')
                    
                      def onKeyDown(key, repeat, flags, view)
                        buffer = 0.chr*256
                        char = 0.chr('UTF-8')*2 # maintain UTF-8 encoding
                        GetKeyboardState.call(buffer)
                        res = ToUnicode.call(key, repeat, buffer, char, char.size, 0)
                        #res = ToAscii.call(key, repeat, buffer, char, 0)
                        return if res == 0
                        char.strip!
                        p char
                        # process
                      end
                    
                    end
                    
                    unless file_loaded?(__FILE__)
                      UI.menu('Plugins').add_item('Key Test'){
                        Sketchup.active_model.select_tool(KeyTest.new)
                      }
                    end
                    
                    
                    

                    You need Win32API.so for SU2013 and below though.


                    Place in the Plugins folder. Access: Plugins > Key Test

                    1 條回覆 最後回覆 回覆 引用 0
                    • A 離線
                      Anton_S
                      最後由 編輯

                      @dan rathbun said:

                      . That'll raise a NameError exception ("uninitialized constant UTF".)

                      Oh 😆 Fixed

                      1 條回覆 最後回覆 回覆 引用 0
                      • tt_suT 離線
                        tt_su
                        最後由 編輯

                        @dan rathbun said:

                        Err.. found a Ruby Core bug.
                        Even after setting Encoding::default_internal="UTF-8"
                        the Integer#chr() method does not return a string in the default internal encoding.

                        sigh "Unicode support" grumble kicks Ruby on the shin

                        1 條回覆 最後回覆 回覆 引用 0
                        • Dan RathbunD 離線
                          Dan Rathbun
                          最後由 編輯

                          Yea did you look at the C code by clicking the method in the CHM ?
                          It has a kind of convoluted switch statement.

                          I'm not here much anymore.

                          1 條回覆 最後回覆 回覆 引用 0
                          • 1
                          • 2
                          • 2 / 2
                          • 第一個貼文
                            最後的貼文
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement